How to concatenate string variables in Bash

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

    In my opinion, the simplest way to concatenate two strings is to write a function that does it for you, then use that function.

    function concat ()
    {
        prefix=$1
        suffix=$2
    
        echo "${prefix}${suffix}"
    }
    
    foo="Super"
    bar="man"
    
    concat $foo $bar   # Superman
    
    alien=$(concat $foo $bar)
    
    echo $alien        # Superman
    

提交回复
热议问题