How to Concatenate String Variables in Bash?

The “concatenation” is a process of joining two or more strings in the form of appending a string at the right end of another string. It is useful to generate a new string by joining multiple strings.

Like other programming languages, “Bash” also assists the users to concatenate the variables containing whether, numeric, string, or alphanumeric values. The variables that contain the alphanumeric values, i.e, both integer and string, are called string variables.

This post demonstrates all the possible methods to concatenate string variables in Bash.

  • Placing String Sequentially
  • Using Addition Assignment Operator (+=)

Method 1: Placing String Sequentially

The simplest method to concatenate the string variables is to place them one after the other in sequence. As a result of this operation, all the values of string variables will be merged and displayed as a concatenated string. 

Let’s check how could you do that with the help of various possible examples.

Example 1: Concatenate a Single String

A bash “script.sh” is opened in a nano editor having the following lines of code:

$ nano script.sh

Code

#!/bin/bash
str1="Hello,"
str2="itslinuxfoss"
concat="$str1$str2"
echo "$concat"

The script.sh is explained here:

  • The “#!/bin/bash represents the “Bash Shebang” that will run the current script in bash shell.
  • The “str1” and “str2” are variables containing different string values.
  • The “concat” is the third variable that stores the value of “$str1” and “$str2” sequentially.
  • In last, “echo” statement is defined to print the “concat” variable value.

Use the “Ctrl+S” key for saving and “Ctrl+X” for exiting the editor.

Execute the “script.sh” with the help of “dot(.)” operator and check its output:

./script1.sh

The output shows that string variables “str1” and “str2” have been concatenated.

Example 2:  Concatenate Multiple Strings

Apart from two strings, multiple strings can also be concatenated by placing them one after another in a variable. Let’s do this using the modified “script.sh” code:

Code

#!/bin/bash
str1="Welcome"
str2=" to"
str3=" itsLinuxfoss"
str4=" website"
concat="$str1$str2$str3$str4"
echo "$concat"

For instance, there are four sting variables are declared and can be enclosed in the “concat” variable:

Save and close the nano editor.

Run the scrip.sh in the terminal:

./script1.sh

The output contains a concatenated string separated by one tab space character placed between them during their declaration.

Example 3: Concatenate Alphanumeric String

The string variables contain both string and numeric values; that’s why it is also called an “alphanumeric” string. Here it is declared in the “scr2” bash script:

#!/bin/bash
var1="Linux"
var2=" Distribution"
var3=" Ubuntu22.04"
result="$var1$var2$var3"
echo "$result"

The above code block contains total four variables in which the “var3” is called string variable i.e alphanumeric value “Ubuntu22.04”

The execution of the “scr2.sh” will show its output in the terminal:

./scr2.sh

The “string variable” has successfully concatenated with other variables of “the script.sh”

Method 2: Using Addition Assignment Operator (+=)

The “+=” addition assignment is the bash arithmetic operator for the concatenation of two variables. The variables may contain either integer or string values. In addition, it also helps in appending and arrays.

This method follows a set of examples to use the “+=” operator for concatenating string variables.

Example 1: Concatenate a String 

An existing “sample.sh” script is taken as an example in the “nano” editor:

$ nano sample.sh

Code

#!/bin/bash
var="Hello"
var+="Linux"
echo "$var"

The above code block contains the “var” variable having one string “Hello” and then append with the “Linux” string i.e “var+=”:

Execute the “sample.sh” script:

./sample.sh

The “sample.sh” script has successfully executed showing the concatenated string “HelloLinxu”.

Example 2: Concatenate a Numeric String

The “+=” is an arithmetic operator and also assists the user to concatenate the variables having integer values. Its format is the same as for string variables:

Code

#!/bin/bash
var="20"
var+="23"
echo "$var"

At this time the “var” variable stores an integer value that will append with another integer value declared using “+=” operator:

Check the “sample.sh” script output by executing it:

./sample.sh

The variable “var” integer values have been concatenated.

Example 3: Concatenate a Literal String

The two or more strings can be concatenated using literal strings. In this string, the first string variable encloses in curly braces, follows by another string variable. 

The complete string should be enclosed in the curly braces to avoid character mistakes. Its practical implementation is explained via the “sample.sh” script.

Code

#!/bin/bash
var1="Hello"
var2="${var1}Linux"
echo "$var2"

The “sample.sh” contains:

  • var1: First variable that stores the string “Hello”.
  • var2: Second variable that encloses “${var1}” variable name and “Linux” string.
  • echo: Prints the “$var2” variable:

Run the “sample.sh” bash script:

./sample.sh

The string variables have been concatenated.

Example 4: Concatenate a String Variable in Bash Using “for” Loop

The “+=” operator can also be implemented in the “for” loop. The “for” loop will iterate on the list of items and concatenate the string in a sequence. Let’s perform this task.

Code

#!/bin/bash
a=""
for Distro in 'Ubuntu' 'RHEL' 'CentOS' 'Fedora'; do
  a+="${Distro} "
done

echo "$a"

The code block description is stated here:

  • The variable “a” contains an empty string.
  • The “for” loop defines a list “Distro” having four string values.
  • The “a+=” concatenates the “Distro” list values one by one.
  • The “do” and “done” represents the starting and ending statement of the “for” loop.
  • The “echo” statement prints the “$a” variable concatenated string:

Execute the script and check the “for” loop output:

./func.sh

The “for” loop has successfully printed the concatenated list of items.

Conclusion

The string variables can be easily concatenated by “placing them sequentially” and using the “+=” operator in the bash shell. These operations are useful for concatenating multiple string variables at once. Both these operations can also be used in the “for” loop.

This post has covered all possible methods to concatenate string variables in Bash.