How do you append to an already existing string?

后端 未结 7 529
孤城傲影
孤城傲影 2020-12-12 16:38

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\"
$t         


        
相关标签:
7条回答
  • 2020-12-12 17:15
    VAR=$VAR"$VARTOADD(STRING)"   
    echo $VAR
    
    0 讨论(0)
  • 2020-12-12 17:15
    #!/bin/bash
    
    msg1=${1} #First Parameter
    msg2=${2} #Second Parameter
    
    concatString=$msg1"$msg2" #Concatenated String
    concatString2="$msg1$msg2"
    
    echo $concatString 
    echo $concatString2
    
    0 讨论(0)
  • 2020-12-12 17:21

    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
    
    0 讨论(0)
  • 2020-12-12 17:25
    $ string="test"
    $ string="${string}test2"
    $ echo $string
    testtest2
    
    0 讨论(0)
  • 2020-12-12 17:29
    #!/bin/bash
    message="some text"
    message="$message add some more"
    
    echo $message
    

    some text add some more

    0 讨论(0)
  • 2020-12-12 17:31
    teststr=$'test1\n'
    teststr+=$'test2\n'
    echo "$teststr"
    
    0 讨论(0)
提交回复
热议问题