重击:重复字符多次 [英] Bash: repeat character a variable number of times
问题描述
根据以下问题和反省:
https://unix.stackexchange.com/questions/188658/使用-printf命令编写一个字符n次-
和
我想学习如何设置一个字符/字符串的重复值.例如,以下代码非常有效:
I would like to learn how one might go about parameterizing the repeat value for a character/string. For example, the followings works spiffingly:
printf " ,\n%0.s" {1..5}
但是,如果我想参数化"5",请说:
However, if I wanted to parameterize '5', say:
num=5
我似乎无法正确进行扩展才能完成此工作.例如:
I cannot seem to get the expansion correct to make this work. For instance:
printf " ,\n%0.s" {1..$((num))}
失败.
任何想法/想法都将受到欢迎-我认为有一种方法可以做到这一点而不必求助于perl或awk,所以只是对poss感到好奇.
Any thoughts/ideas would be most welcome - I reckon there's a way to do this without having to resort to perl or awk so just curious if poss.
谢谢!
推荐答案
如果可以将命令构建为字符串(具有所需的所有参数扩展),则可以对其进行评估.这将打印X num
次:
If you can build the command as a string -- with all the parameter expansion you want -- then you can evaluate it. This prints X num
times:
num=10
eval $(echo printf '"X%0.s"' {1..$num})
这篇关于重击:重复字符多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!