你如何追加到一个已经存在的字符串? [英] How do you append to an already existing string?

查看:110
本文介绍了你如何追加到一个已经存在的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要追加到一个字符串,以便在它每次我环路将增加说测试字符串。

I want append to a string so that every time I loop over it will add say "test" to the string.

像PHP你会做:

$teststr = "test1\n"
$teststr .= "test2\n"
echo = "$teststr"

回声:

test1
test2

但我必须这样做在shell脚本

But I need to do this in a shell script

推荐答案

在经典SH,你必须做的是这样的:

In classic sh, you have to do something like:

s=test1
s=$s"test2"

(有很多关于这一主题的变化,如S =$ {S}测试2)

(there are lots of variations on that theme, like s="${s}test2")

在bash中,你可以使用+ =:

In bash, you can use +=:

s=test1
s+=test2

这篇关于你如何追加到一个已经存在的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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