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
Software Requirements and Conventions Used
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
- The most basic string concatenation in bash is by joining two or more strings in a single
echo
statement. Consider the following example:
Output:#!/bin/bash STRING1="String" STRING2="Concatenation" echo $STRING1 $STRING2
$ ./concat.sh String Concatenation
Using the above bash script example we have concatenated two string variablesSTRING1
andSTRING2
into a unified output by using theecho
command. The same principle can by expanded into a multiple string concatenation:
Output:#!/bin/bash STRING1="Bash" STRING2="String" STRING3="Concate" STRING4="nation" echo $STRING1 Scripting $STRING2 $STRING3$STRING4
$ ./concat.sh Bash Scripting String Concatenation
- In the next example we are going to use perform string concatenation with variable using curly braces
{}
:
Output:#!/bin/bash STRING1="ing" STRING2="Concate" echo "Str${STRING1} ${STRING2}nation"
$ ./concat.sh String Concatenation
Curly braces allow to concatenate string and variable without space. - 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:
Output:#!/bin/bash STRING1="Today is:" echo $STRING1 `date`
$ ./concat.sh Today is: Fri 27 Nov 2020 14:17:11 AEDT
- In Bash it is also possible to use the
+=
arithmetic operator to join two strings together. Consider a following example:
Output:#!/bin/bash STRING1="Today is:" STRING1+=" " STRING1+=`date` echo $STRING1
$ ./concat.sh Today is: Fri 27 Nov 2020 14:26:17 AEDT
- Next example will explain bash string concatenation using for loop. To start you can write something simple as the following:
Output:#!/bin/bash STRING1="Bash" STRING2="String" for i in $STRING1 $STRING2 "Concatenation"; do output+="$i " done echo $output
$ ./concat.sh Bash String Concatenation
Subscribe to RSS and NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.
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. Subscribe to RSS and NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.