How to concatenate string variables in Bash

后端 未结 30 1909
攒了一身酷
攒了一身酷 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:56

    Even if the += operator is now permitted, it has been introduced in Bash 3.1 in 2004.

    Any script using this operator on older Bash versions will fail with a "command not found" error if you are lucky, or a "syntax error near unexpected token".

    For those who cares about backward compatibility, stick with the older standard Bash concatenation methods, like those mentioned in the chosen answer:

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

提交回复
热议问题