How to concatenate string variables in Bash

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

    The way I'd solve the problem is just

    $a$b
    

    For example,

    a="Hello"
    b=" World"
    c=$a$b
    echo "$c"
    

    which produces

    Hello World
    

    If you try to concatenate a string with another string, for example,

    a="Hello"
    c="$a World"
    

    then echo "$c" will produce

    Hello World
    

    with an extra space.

    $aWorld
    

    doesn't work, as you may imagine, but

    ${a}World
    

    produces

    HelloWorld
    

提交回复
热议问题