How to concatenate string variables in Bash

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

    Safer way:

    a="AAAAAAAAAAAA"
    b="BBBBBBBBBBBB"
    c="CCCCCCCCCCCC"
    d="DD DD"
    s="${a}${b}${c}${d}"
    echo "$s"
    AAAAAAAAAAAABBBBBBBBBBBBCCCCCCCCCCCCDD DD
    

    Strings containing spaces can become part of command, use "$XXX" and "${XXX}" to avoid these errors.

    Plus take a look at other answer about +=

提交回复
热议问题