重复一个字符串N次

3
在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

2
printf -- "${Test2}%.s" {1..5} - HatLess
如果 $Test2 中可能包含 %\ 或其他元字符,那么这样做是很危险的,因为 printf 会进行解释。 - John Kugelman
1个回答

3

没有等效的速记方法。您可以使用显式循环来完成,如下所示:

test=""
for ((i=0; i<5; i++)); do test+="----"; done

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接