无法使用 --help 和 printf

无法使用 --help 和 printf

大多数程序都会打印使用情况并使用“--help”退出。但我无法让它与以下程序一起工作printf

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

man 1 printf说:

概要

       printf格式 [参数]...

       printf选项

描述

       根据格式打印参数,或者根据选项执行:

       - 帮助显示此帮助并退出

也没有什么错coreutils 源代码

  /* 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;
        }
    }

我为什么做不到printf --help

答案1

有两种类型printf。一种是由 coreutils 提供的,另一种是由 Bash 作为 shell 内置提供的。

$ 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:
...

为了获得有关 Bash 内置的帮助,请使用以下命令help

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

答案2

有两种 printfs 可供您使用:shell 内置命令和可执行文件。shell 内置命令在 中描述man bash。它不支持--help。但是,您可以使用 获得有关它的信息help printf

man 1 printf描述/usr/bin/printf并且确实支持--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'

答案3

printfbash也是内置的shell ( )。因此,当您运行

printf --help

执行内置函数printf是因为默认情况下内置函数总是优先于外部函数并且它没有--help选项,因此出现错误。

查找所有可用的printf可执行文件:

type -a printf

它将按优先顺序显示可执行文件。

您可以通过以下方式检查help内置页面printf

help printf

另一方面,如果您想运行外部printf,请执行以下操作之一:

command printf
"printf"
'printf'
\printf

相关内容