How to concatenate string variables in Bash

后端 未结 30 1912
攒了一身酷
攒了一身酷 2020-11-22 03:40

In PHP, strings are concatenated together as follows:

$foo = \"Hello\";
$foo .= \" World\";

Here, $foo becomes \"Hello World\"

30条回答
  •  失恋的感觉
    2020-11-22 03:52

    I wanted to build a string from a list. Couldn't find an answer for that so I post it here. Here is what I did:

    list=(1 2 3 4 5)
    string=''
    
    for elm in "${list[@]}"; do
        string="${string} ${elm}"
    done
    
    echo ${string}
    

    and then I get the following output:

    1 2 3 4 5
    

提交回复
热议问题