Bash : Duplicate a string variable n times [duplicate]

允我心安 提交于 2019-12-11 15:39:22

问题


I have a variable : str='abc'

I would like to copy it n times, with a space between each string, so the echo gives, for n=3 :

echo $str
abc abc abc

I don't want a space after the last abc

I've already seen the link mentionned but they doesn't help. The last answer from @Amadan is the good one thanks, but I can't accept it :/


回答1:


One more way:

eval echo {1..$n} | sed "s/[[:digit:]]*/${var}/g"

Example:

n=3
var=abc

eval echo {1..$n} | sed "s/[[:digit:]]*/${var}/g"
abc abc abc


来源:https://stackoverflow.com/questions/56835374/bash-duplicate-a-string-variable-n-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!