A maioria dos programas imprime o uso e sai com "--help". Mas não consigo fazê-lo funcionar com printf
:
$ printf --help
bash: printf: --: invalid option
printf: usage: printf [-v var] format [arguments]
man 1 printf
diz:
SINOPSE
imprimirFORMATAR [ARGUMENTO]...
imprimirOPÇÃO
DESCRIÇÃO
Imprima ARGUMENT(s) de acordo com FORMAT, ou execute de acordo com OPTION:
--ajudaexiba esta ajuda e saia
Também não há nada de errado com ocódigo fonte 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;
}
}
Por que não posso fazer printf --help
?
Responder1
Existem dois tipos de printf
. Aquele fornecido pelo coreutils e aquele fornecido pelo Bash como um shell integrado.
$ 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:
...
Para obter ajuda sobre o recurso integrado do Bash, use o help
comando:
$ help printf
printf: printf [-v var] format [arguments]
Formats and prints ARGUMENTS under control of the FORMAT.
...
Responder2
Existem dois printfs disponíveis para você: o shell integrado e um executável. O shell interno é descrito em man bash
. Não suporta --help
. Você pode, no entanto, obter informações sobre isso com help printf
.
man 1 printf
descreve /usr/bin/printf
e, de fato, suporta --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'
Responder3
printf
também é um shell ( bash
) integrado. Então, quando você corre
printf --help
o embutido printf
é executado porque por padrão os embutidos sempre têm precedência sobre os externos e não tem --help
opção, daí o erro.
Para encontrar todos os printf
executáveis disponíveis:
type -a printf
Ele mostrará os executáveis em ordem de precedência.
Você pode verificar a help
página do built-in printf
:
help printf
Por outro lado, se você quiser executar o external printf
, siga um destes procedimentos:
command printf
"printf"
'printf'
\printf