How to concatenate string variables in Bash

后端 未结 30 1508
攒了一身酷
攒了一身酷 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 04:02

    I prefer to use curly brackets ${} for expanding variable in string:

    foo="Hello"
    foo="${foo} World"
    echo $foo
    > Hello World
    

    Curly brackets will fit to Continuous string usage:

    foo="Hello"
    foo="${foo}World"
    echo $foo
    > HelloWorld
    

    Otherwise using foo = "$fooWorld" will not work.

提交回复
热议问题