需要建立一個字符串foobar is not foo and not bar
。如何在bash中獲得printf格式的特定參數?
我期待類似的代碼:
$ printf "%1%2 is not %1 and not %2" 'foo' 'bar'
那麼,如何通過一個特定的參數?
我ENV:
$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
需要建立一個字符串foobar is not foo and not bar
。如何在bash中獲得printf格式的特定參數?
我期待類似的代碼:
$ printf "%1%2 is not %1 and not %2" 'foo' 'bar'
那麼,如何通過一個特定的參數?
我ENV:
$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
的語法是C相同printf函數。
$ printf '%1$s%2$s is not %1$s and not %2$s' 'foo' 'bar'
其實如果你只是需要連接一些字符串,你不需要printf
。您可以在字符串中使用命名變量:
a=foo
b=bar
echo "$a$b is not $a and not $b"
'printf'接受與C中'printf'相同的格式化字符串以及一些其他對shell腳本有用的選項。其中一些附加選項也是依賴於外殼的。但是,似乎在格式字符串中的多個位置重複參數不受支持或不可移植。你應該通過重複的論證兩次,每個位置一次。 –