How to concatenate string variables in Bash

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

    You can concatenate without the quotes. Here is an example:

    $Variable1 Open
    $Variable2 Systems
    $Variable3 $Variable1$Variable2
    $echo $Variable3
    

    This last statement would print "OpenSystems" (without quotes).

    This is an example of a Bash script:

    v1=hello
    v2=world
    v3="$v1       $v2"
    echo $v3            # Output: hello world
    echo "$v3"          # Output: hello       world
    
    0 讨论(0)
  • 2020-11-22 03:59
    foo="Hello"
    foo="${foo} World"
    echo "${foo}"
    > Hello World
    

    In general to concatenate two variables you can just write them one after another:

    a='Hello'
    b='World'
    c="${a} ${b}"
    echo "${c}"
    > Hello World
    
    0 讨论(0)
  • 2020-11-22 03:59
    a="Hello,"
    a=$a" World!"
    echo $a
    

    This is how you concatenate two strings.

    0 讨论(0)
  • 2020-11-22 04:00

    Yet another approach...

    > H="Hello "
    > U="$H""universe."
    > echo $U
    Hello universe.
    

    ...and yet yet another one.

    > H="Hello "
    > U=$H"universe."
    > echo $U
    Hello universe.
    
    0 讨论(0)
  • 2020-11-22 04:00

    There are voiced concerns about performance, but no data is offered. Let me suggest a simple test.

    (NOTE: date on macOS does not offer nanoseconds, so this must be done on Linux.)

    I have created append_test.sh on GitHub with the contents:

    #!/bin/bash -e
    
    output(){
        ptime=$ctime;
        ctime=$(date +%s.%N);
        delta=$(bc <<<"$ctime - $ptime");
        printf "%2s. %16s chars  time: %s  delta: %s\n" $n "$(bc <<<"10*(2^$n)")" $ctime $delta;
    }
    
    method1(){
        echo 'Method: a="$a$a"'
        for n in {1..32}; do a="$a$a"; output; done
    }
    
    method2(){
        echo 'Method: a+="$a"'
        for n in {1..32}; do a+="$a";  output; done
    }
    
    ctime=0; a="0123456789"; time method$1
    

    Test 1:

    $ ./append_test.sh 1
    Method: a="$a$a"
     1.               20 chars  time: 1513640431.861671143  delta: 1513640431.861671143
     2.               40 chars  time: 1513640431.865036344  delta: .003365201
     3.               80 chars  time: 1513640431.868200952  delta: .003164608
     4.              160 chars  time: 1513640431.871273553  delta: .003072601
     5.              320 chars  time: 1513640431.874358253  delta: .003084700
     6.              640 chars  time: 1513640431.877454625  delta: .003096372
     7.             1280 chars  time: 1513640431.880551786  delta: .003097161
     8.             2560 chars  time: 1513640431.883652169  delta: .003100383
     9.             5120 chars  time: 1513640431.886777451  delta: .003125282
    10.            10240 chars  time: 1513640431.890066444  delta: .003288993
    11.            20480 chars  time: 1513640431.893488326  delta: .003421882
    12.            40960 chars  time: 1513640431.897273327  delta: .003785001
    13.            81920 chars  time: 1513640431.901740563  delta: .004467236
    14.           163840 chars  time: 1513640431.907592388  delta: .005851825
    15.           327680 chars  time: 1513640431.916233664  delta: .008641276
    16.           655360 chars  time: 1513640431.930577599  delta: .014343935
    17.          1310720 chars  time: 1513640431.954343112  delta: .023765513
    18.          2621440 chars  time: 1513640431.999438581  delta: .045095469
    19.          5242880 chars  time: 1513640432.086792464  delta: .087353883
    20.         10485760 chars  time: 1513640432.278492932  delta: .191700468
    21.         20971520 chars  time: 1513640432.672274631  delta: .393781699
    22.         41943040 chars  time: 1513640433.456406517  delta: .784131886
    23.         83886080 chars  time: 1513640435.012385162  delta: 1.555978645
    24.        167772160 chars  time: 1513640438.103865613  delta: 3.091480451
    25.        335544320 chars  time: 1513640444.267009677  delta: 6.163144064
    ./append_test.sh: fork: Cannot allocate memory
    

    Test 2:

    $ ./append_test.sh 2
    Method: a+="$a"
     1.               20 chars  time: 1513640473.460480052  delta: 1513640473.460480052
     2.               40 chars  time: 1513640473.463738638  delta: .003258586
     3.               80 chars  time: 1513640473.466868613  delta: .003129975
     4.              160 chars  time: 1513640473.469948300  delta: .003079687
     5.              320 chars  time: 1513640473.473001255  delta: .003052955
     6.              640 chars  time: 1513640473.476086165  delta: .003084910
     7.             1280 chars  time: 1513640473.479196664  delta: .003110499
     8.             2560 chars  time: 1513640473.482355769  delta: .003159105
     9.             5120 chars  time: 1513640473.485495401  delta: .003139632
    10.            10240 chars  time: 1513640473.488655040  delta: .003159639
    11.            20480 chars  time: 1513640473.491946159  delta: .003291119
    12.            40960 chars  time: 1513640473.495354094  delta: .003407935
    13.            81920 chars  time: 1513640473.499138230  delta: .003784136
    14.           163840 chars  time: 1513640473.503646917  delta: .004508687
    15.           327680 chars  time: 1513640473.509647651  delta: .006000734
    16.           655360 chars  time: 1513640473.518517787  delta: .008870136
    17.          1310720 chars  time: 1513640473.533228130  delta: .014710343
    18.          2621440 chars  time: 1513640473.560111613  delta: .026883483
    19.          5242880 chars  time: 1513640473.606959569  delta: .046847956
    20.         10485760 chars  time: 1513640473.699051712  delta: .092092143
    21.         20971520 chars  time: 1513640473.898097661  delta: .199045949
    22.         41943040 chars  time: 1513640474.299620758  delta: .401523097
    23.         83886080 chars  time: 1513640475.092311556  delta: .792690798
    24.        167772160 chars  time: 1513640476.660698221  delta: 1.568386665
    25.        335544320 chars  time: 1513640479.776806227  delta: 3.116108006
    ./append_test.sh: fork: Cannot allocate memory
    

    The errors indicate that my Bash got up to 335.54432 MB before it crashed. You could change the code from doubling the data to appending a constant to get a more granular graph and failure point. But I think this should give you enough information to decide whether you care. Personally, below 100 MB I don't. Your mileage may vary.

    0 讨论(0)
  • 2020-11-22 04:00

    Despite of the special operator, +=, for concatenation, there is a simpler way to go:

    foo='Hello'
    foo=$foo' World'
    echo $foo
    

    Double quotes take an extra calculation time for interpretation of variables inside. Avoid it if possible.

    0 讨论(0)
提交回复
热议问题