[英]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"
...但顯然不是這樣。
Hi John, welcome to The Icecream Factory. The Icecream Factory is a great place to work. Thanks John.
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.