在Python中,可以使用“乘法”操作符来重复一个字符串多次:
test="----"
print(test)
test="----"*10
print(test)
#output
----
----------------------------------------
在bash中有等效的命令吗?我尝试使用*
,但它不起作用:
$ Test2="----"
$ echo $Test2
----
$ echo ${Test2}*5
----*5
$ echo $Test2*5
----*5
$ echo echo $[Test2]*5
-bash: ----: syntax error: operand expected (error token is "-")
$ echo $(Test2)*5
Test2: command not found
printf -- "${Test2}%.s" {1..5}
- HatLess$Test2
中可能包含%
、\
或其他元字符,那么这样做是很危险的,因为printf
会进行解释。 - John Kugelman