我在哪里可以找到有关 bash printf 的一些好的文档?

evi*_*oup 4 bash documentation utilities printf

man printfinfo printf而且printf --help对我来说都没什么用;我想要一个FORMAT选项的解释。例如,我可以使用:

printf '%s\n' foo bar
Run Code Online (Sandbox Code Playgroud)

并获得输出:

foo
bar
Run Code Online (Sandbox Code Playgroud)

从在线的一般阅读中,我推测这%s意味着类似于“分隔符”,因此它会printf打印提供给它的每个参数,并用换行符分隔。

我还在这里和那里看到了一些参考资料%d(我认为这与数字有关?)。有人可以提供这些格式选项的概述吗?

Jos*_*Gee 11

对于 bash,主要资源是man bash. 特别是对于内置函数,有help内置函数。这是引自help printf.

除了 printf(1) 和 printf(3) 中描述的标准格式规范之外, printf 解释:

 %b        expand backslash escape sequences in the corresponding argument
 %q        quote the argument in a way that can be reused as shell input
 %(fmt)T output the date-time string resulting from using FMT as a format
       string for strftime(3)
Run Code Online (Sandbox Code Playgroud)

中使用的printf(N)符号表示help printf您应该参考由括号中的数字表示的命令的手册部分。

如果需要特殊的 %(fmt)T 说明符,请参阅man 1 printf, man 3 printf, 和man 3 strftime

你必须自己拼凑起来;文档并没有简单地列出“%s 是字符串,%d 是数字。” 我相信您要求的主要内容是转换规范


slm*_*slm 5

我找到的最好的资源是bash-hackers.org wiki,其中包含与 Bash 相关的所有内容。这是手册页应该是什么,而不是难以导航的大量文件。

有关于内置函数的特定主题,包括printf. 这个页面是详尽的!它包括所有格式选项和示例,甚至底部的讨论部分有助于充实我们的极端情况和细微差别与printf的功能。

我也知道在 printf 上使用GNU Coreutils 文档,特别是关于格式化选项的页面,在紧要关头。