参见英文答案 >
How can I repeat a character in bash?17个答案有没有办法在bash中反复打印相同的字符,就像您可以使用此构造在python中执行此操作:
@H_404_1@print('%' * 3)
给出
@H_404_1@%%%
当然,只需使用printf和一些bash字符串操作
@H_404_1@$ s=$(printf "%-30s" "*")
$ echo "${s// /*}"
******************************
应该有一个较短的方法,但目前我将如何做。您可以将其设置为一个可以存储在库中以供将来使用的功能
@H_404_1@printf_new() { str=$1 num=$2 v=$(printf "%-${num}s" "$str") echo "${v// /*}" }测试运行:
@H_404_1@$ printf_new "*" 20 ******************** $ printf_new "*" 10 ********** $ printf_new "%" 10 %%%%%%%%%%