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 “” is opened in a nano editor having the following lines of code:

$ nano


echo "$concat"

The 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 “” with the help of “dot(.)” operator and check its output:


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 “” code:


str2=" to"
str3=" itsLinuxfoss"
str4=" website"
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 in the terminal:


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:

var2=" Distribution"
var3=" Ubuntu22.04"
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 “” will show its output in the terminal:


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

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 “” script is taken as an example in the “nano” editor:

$ nano


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 “” script:


The “” 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:


echo "$var"

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

Check the “” script output by executing it:


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 “” script.


echo "$var2"

The “” 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 “” bash script:


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.


for Distro in 'Ubuntu' 'RHEL' 'CentOS' 'Fedora'; do
  a+="${Distro} "

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:


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


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.