views:

70

answers:

4

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 = "test1\n"
$teststr .= "test2\n"
echo = "$teststr"

echos:

test1
test2

But I need to do this in a shell script

+2  A: 
teststr=$'test1\n'
teststr+=$'test2\n'
echo "$teststr"
Ignacio Vazquez-Abrams
+4  A: 
$ string="test"
$ string="${string}test2"
$ echo $string
testtest2
ghostdog74
+2  A: 

In classic sh, you have to do something like:

$ s=test1
$ s=$s"test2"

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

In bash, you can use +=:

$ s=test1
$ s+=test2
William Pursell
+1  A: 
#!/bin/bash
message="some text"
message="$message add some more"

echo $message

some text add some more

Jim