'printf -v' 是什么意思?

15

在网上的bash脚本示例和stackoverflow上遇到了多次这个printf -v,但在printf手册页中没有找到适当的解释。

man printfman 3 printf都无法帮助我。

我应该去哪里寻找答案呢?

1个回答

20
在Linux中有几个与 printf 相关的命令:
  1. 已知的C函数 printf (在man 3 printf中有描述)
  2. GNU coreutils printf,位于/usr/bin/printf (参见man 1 printfinfo coreutils printf)
  3. Bash内建的printf (查看SHELL BUILTIN COMMANDS部分,可以在man bashinfo bash printf或使用help printf)。
要找到你确切需要使用的命令,请使用type <command>
root@pi:~# type -a printf
printf is a shell builtin
printf is /usr/bin/printf
printf is /bin/printf

因此,这里的解决方案是第三个。
root@pi:~# help printf
printf: printf [-v var] format [arguments]
    Formats and prints ARGUMENTS under control of the FORMAT.
    
    Options:
      -v var    assign the output to shell variable VAR rather than
            display it on the standard output
    
    FORMAT is a character string which contains three types of objects: plain
    characters, which are simply copied to standard output; character escape
    sequences, which are converted and copied to the standard output; and
    format specifications, each of which causes printing of the next successive
    argument.
    
    In addition to the standard format specifications described in printf(1),
    printf interprets:
    
      %b    expand backslash escape sequences in the corresponding argument
      %q    quote the argument in a way that can be reused as shell input
      %Q    like %q, but apply any precision to the unquoted argument before
            quoting
      %(fmt)T   output the date-time string resulting from using FMT as a format
                string for strftime(3)
    
    The format is re-used as necessary to consume all of the arguments.  If
    there are fewer arguments than the format requires,  extra format
    specifications behave as if a zero value or null string, as appropriate,
    had been supplied.
    
    Exit Status:
    Returns success unless an invalid option is given or a write or assignment
    error occurs.

9
通过运行 help printf 命令可以找到关于 shell 内置命令 printf 的帮助信息。 - chepner
2
@chepner 谢谢,我会将你的答案添加到回答中。 - sjas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接