String concatenation in Bash

This tutorial will explain the Bash string concatenation by using examples. When it comes to bash scripting or programming in general, the concatenation refers to joining two or more string together to produce single unified output. Using Bash shell and bash scripting the string concatenation can be achieved in number for ways.

In this tutorial you will learn:

  • How to concatenate strings using echo command
  • How to concatenate strings in loop
  • How to concatenate string with command output

String concatenation in Bash

String concatenation in Bash

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any GNU/Linux system
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash string concatenation examples

  1. The most basic string concatenation in bash is by joining two or more strings in a single echo statement. Consider the following example:
    #!/bin/bash
    STRING1="String"
    STRING2="Concatenation"
    
    echo $STRING1 $STRING2
    

    Output:

    $ ./concat.sh 
    String Concatenation
    

    Using the above bash script example we have concatenated two string variables STRING1 and STRING2 into a unified output by using the echo command. The same principle can by expanded into a multiple string concatenation:

    #!/bin/bash
    
    STRING1="Bash"
    STRING2="String"
    STRING3="Concate"
    STRING4="nation"
    
    echo $STRING1 Scripting $STRING2 $STRING3$STRING4
    

    Output:

    $ ./concat.sh 
    Bash Scripting String Concatenation
    


  2. In the next example we are going to use perform string concatenation with variable using curly braces {}:
    #!/bin/bash
    
    STRING1="ing"
    STRING2="Concate"
    
    echo "Str${STRING1} ${STRING2}nation"
    

    Output:

    $ ./concat.sh 
    String Concatenation
    

    Curly braces allow to concatenate string and variable without space.

  3. In a yet another bash concatenation example we are going to concatenate string and command output. For example, let’s concatenate string with output of the date command:
    #!/bin/bash
    
    STRING1="Today is:"
    
    echo $STRING1 `date`
    

    Output:

    $ ./concat.sh 
    Today is: Fri 27 Nov 2020 14:17:11 AEDT
    


  4. In Bash it is also possible to use the += arithmetic operator to join two strings together. Consider a following example:
    #!/bin/bash
    
    STRING1="Today is:"
    STRING1+=" "
    STRING1+=`date`
    
    echo $STRING1
    

    Output:

    $ ./concat.sh 
    Today is: Fri 27 Nov 2020 14:26:17 AEDT
    
  5. Next example will explain bash string concatenation using for loop. To start you can write something simple as the following:
    #!/bin/bash
    
    STRING1="Bash"
    STRING2="String"
    
    for i in $STRING1 $STRING2 "Concatenation"; do
        output+="$i "
    done
    
    echo $output
    

    Output:

    $ ./concat.sh 
    Bash String Concatenation
    

Conclusion

Bash string concatenation is a must have knowledge for any even beginning bash scripting user. Fortunately, it is easy to understand and implement. Be careful when using any special character such as single quote ' in a string. In this case enclose the string variable in double quote eg. "Let's go" to avoid errors.





Comments and Discussions
Linux Forum