Ubuntu: Can't use --help with printf



Question:

Most programs print usage and quit with "--help". But I can't get it to work with printf:

$ printf --help  bash: printf: --: invalid option  printf: usage: printf [-v var] format [arguments]  

man 1 printf says:

SYNOPSIS

       printf FORMAT [ARGUMENT]...

       printf OPTION

DESCRIPTION

       Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:

       --help display this help and exit

There's also nothing wrong about the coreutils source code:

  /* We directly parse options, rather than use parse_long_options, in       order to avoid accepting abbreviations.  */    if (argc == 2)      {        if (STREQ (argv[1], "--help"))          usage (EXIT_SUCCESS);          if (STREQ (argv[1], "--version"))          {            version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,                         (char *) NULL);            return EXIT_SUCCESS;          }      }  

Why can't I do printf --help?


Solution:1

There's two types of printf. The one provided by coreutils and the one provided by Bash as a shell built-in.

$ type printf  printf is a shell builtin  $ /usr/bin/printf --help  Usage: /usr/bin/printf FORMAT [ARGUMENT]...    or:  /usr/bin/printf OPTION  Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:  ...  

In order to get help about Bash's built-in, use the help command instead:

$ help printf  printf: printf [-v var] format [arguments]      Formats and prints ARGUMENTS under control of the FORMAT.  ...  


Solution:2

There are two printfs available to you: the shell builtin and an executable. The shell builtin is described in man bash. It does not support --help. You can, however, get information about it with help printf.

man 1 printf describes /usr/bin/printf and it does, in fact support --help:

$ /usr/bin/printf --help  Usage: /usr/bin/printf FORMAT [ARGUMENT]...    or:  /usr/bin/printf OPTION  Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:          --help     display this help and exit        --version  output version information and exit    FORMAT controls the output as in C printf.  Interpreted sequences are:      \"      double quote    \\      backslash    \a      alert (BEL)    \b      backspace    \c      produce no further output    \e      escape    \f      form feed    \n      new line    \r      carriage return    \t      horizontal tab    \v      vertical tab    \NNN    byte with octal value NNN (1 to 3 digits)    \xHH    byte with hexadecimal value HH (1 to 2 digits)    \uHHHH  Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)    \UHHHHHHHH  Unicode character with hex value HHHHHHHH (8 digits)    %%      a single %    %b      ARGUMENT as a string with '\' escapes interpreted,            except that octal escapes are of the form \0 or \0NNN    and all C format specifications ending with one of diouxXfeEgGcs, with  ARGUMENTs converted to proper type first.  Variable widths are handled.    NOTE: your shell may have its own version of printf, which usually supersedes  the version described here.  Please refer to your shell's documentation  for details about the options it supports.    GNU coreutils online help: <http://www.gnu.org/software/coreutils/>  Full documentation at: <http://www.gnu.org/software/coreutils/printf>  or available locally via: info '(coreutils) printf invocation'  


Solution:3

printf is a shell (bash) built-in too. So when you run

printf --help  

the built-in printf is executed because by default the built-in ones always get precedence over the external ones and it doesn't have --help option, hence the error.

To find all the available printf executables:

type -a printf  

It will show the executables in the order of precedence.

You can check the help page of built-in printf by:

help printf  

On the other hand if you want to run the external printf, do any one of the following:

command printf  "printf"  'printf'  \printf  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »