無法將 --help 與 printf 一起使用

無法將 --help 與 printf 一起使用

大多數程式都會列印用法並使用“--help”退出。但我無法讓它工作printf

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

man 1 printf說:

概要

       列印函數格式[參數]...

       列印函數選項

描述

       根據 FORMAT 列印 ARGUMENT(s),或依 OPTION 執行:

       - 幫助顯示此幫助並退出

也沒有什麼問題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

有兩種 printf 可供您使用:內建 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

相關內容