Bash:将字符串变量复制n次 [英] Bash : Duplicate a string variable n times

查看:158
本文介绍了Bash:将字符串变量复制n次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量:str='abc'

我想将其复制n次,每个字符串之间都有一个空格,因此echo给出n = 3的信息:

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

我不想在最后一个abc

我已经看到了提到的链接,但它们无济于事. @Amadan的最后一个答案是很好的感谢,但我不能接受:/

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 :/

推荐答案

另一种方法:

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

示例:

n=3
var=abc

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

这篇关于Bash:将字符串变量复制n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆