How do you append to an already existing string?

How do you append to an already existing string?



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



Like in PHP you would do:


$teststr = "test1n"
$teststr .= "test2n"
echo = "$teststr"



echos:


test1
test2



But I need to do this in a shell script




6 Answers
6



In classic sh, you have to do something like:


s=test1
s="$stest2"



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


s="$s""test2"



In bash, you can use +=:


s=test1
s+=test2


$ string="test"
$ string="$stringtest2"
$ echo $string
testtest2


#!/bin/bash
message="some text"
message="$message add some more"

echo $message



some text add some more





this seems to be the most easy to read method IMO
– zeros-and-ones
Jan 24 '17 at 18:46


teststr=$'test1n'
teststr+=$'test2n'
echo "$teststr"


VAR=$VAR"$VARTOADD(STRING)"
echo $VAR


#!/bin/bash

msg1=$1 #First Parameter
msg2=$2 #Second Parameter

concatString=$msg1"$msg2" #Concatenated String
concatString2="$msg1$msg2"

echo $concatString
echo $concatString2






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)