繁体   English   中英

Bash:在 output 中重用 printf 变量

[英]Bash: Reuse printf variables in output

是否可以在格式化中多次使用传递给printf的变量?

例如,使用这一行:

printf 'Hi %s, welcome to %s. %s is a great place to work. Thanks %s.' "John" "The Icecream Factory"

如何“重用” printf中的第一个和第二个变量?

我在想类似的事情:

printf 'Hi %s[1], welcome to %s[2]. %s[1] is a great place to work. Thanks %s[2].' "John" "The Icecream Factory"

...但显然不是这样。

所需 output

Hi John, welcome to The Icecream Factory. The Icecream Factory is a great place to work. Thanks John.

实际 output

Hi John, welcome to The Icecream Factory.  is a great place to work. Thanks .

工作环境为bash 20 中的 bash。

While I don't think it's possible using either the built-in bash implementation of printf or the freestanding GNU printf(1) program, if you can target zsh instead, its version of printf supports POSIX-style printf(3) argument indexing:

通常,转换规范按顺序应用于每个参数,但它们可以通过将%替换为%n$*替换为*n$来明确指定要使用的第n个参数。 建议您不要将这种显式样式的引用与普通样式混合,并且此类混合 styles 的处理可能会在未来发生变化。

$ printf 'Hi %1$s, welcome to %2$s. %2$s is a great place to work. Thanks %1$s.\n' "John" "The Icecream Factory"
Hi John, welcome to The Icecream Factory. The Icecream Factory is a great place to work. Thanks John.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM