Concatenate Strings In Bash: How To Join Strings

Whenever you learn a programming language, one of the first things you will probably be asked to do is concat string variables. Concatenation may sound like a complex word, but it is just jargon for "joining" or "appending" variables to each other. In Bash, you can concatenate two or multiple string literals and variables in multiple different ways.

String Concatenation: The Easy Way

If you have two strings or variables that you want to concat, the easiest way to do this in bash is by writing the variables one after the other as highlighted below.

VAR1="Hello"
VAR2="World"

VAR3="$VAR1 $VAR2"

echo $VAR3

We wrap the concatenated string in double quotes as it is generally considered good practice to wrap variable expansions as such.

The resulting output will be:

# echo prints the resulting string variable VAR3

Hello World

String Concatenation With Literal Strings

If you want to join two strings where one is a literal and another is a variable, you can do so by following a similar pattern as outlined above.

For example:

VAR1="Hello"
VAR2="${VAR1} World"

echo $VAR2

To demarcate the difference between the variable and a string literal, we enclose the variable VAR1 in curly braces. The resulting output will be,

Hello World

Concatenating Multiple Variables

This method also works for joining multiple variables. Simply place the variables one after the other in the order in which you want them and assign the resulting string to a new variable.

VAR1="The"
VAR2="quick brown"
VAR3="lazy dog"

VAR4="${VAR1} ${VAR2} fox jumped over the ${VAR3}"

echo $VAR4

Output:

The quick brown fox jumped over the lazy dog

Concatenating Numbers and Strings

This method is also useful if you want to join a string variable with a number.

VAR1=2
VAR2="quick brown"
VAR3="lazy dog"

VAR4="${VAR1} ${VAR2} foxes jumped over the ${VAR3}"

echo $VAR4

Output:

2 quick brown foxes jumped over the lazy dog

Bash treats a numeric variable or literal as a string depending on the context, as in this case.


String Concatenation With The += Operator

As of Bash version 3.1, a second method can be used to concatenate strings by using the += operator. The operator is generally used to append numbers and strings, and other variables to an existing variable.

VAR1="Hello"
VAR2="World"

VAR1+=" ${VAR2}"

echo VAR1

Output:

Hello World

In this method, the += is essentially shorthand for saying "add this to my existing variable".

Appending Numeric Strings

This method works just as well on numeric strings.

a=5
a+=6
echo $a

Output:

56

If instead, you wanted to treat this as a numeric expression, you would have to wrap it in $(( )) arithmetic expansion.

 NUM=5
 x=6
 (( NUM += x ))
 
 echo $NUM 

Output:

11

Appending Strings In A Loop

In a more complex use case, you might want to cycle through a bunch of variables or literals and concatenate them to form a single string. You can do this by using a for loop.

VAR=""
for CITY in 'London' 'Paris' 'New York'; do
  VAR+="${CITY} "
done

echo $VAR

Output:

London Paris New York

And that's all folks! String concatenation is one of the most important things you will need to have an understanding of to write bash scripts.

Related: How to increment a variable in bash

Bash: How to Increment Variable
Learn how to increment a variable in bash using two different methods