How to Concatenate Strings in Bash

Bash Concatenate Strings

String concatenation is the process of joining two or more strings together to form a single string.

In this tutorial we’ll explain and exemplify different methods to concatenate strings in Bash.

How to Concatenate Strings in Bash

In Bash, strings can be concatenated by placing them one after the other, or by using the += operator. There are a few things to take into account when placing them one after the other, which we’ll cover in this tutorial.

The simplest way to concatenate strings in Bash is by just placing two or more strings one after the other.

Concatenate String Variables

In this example we’ll concatenate 2 string variables.

Variables a and b are strings, and we’re concatenating them in c simply by putting them one next to the other.

#!/bin/bash

a="Hello, "
b="World!"

c="$a$b"

echo $c
Output
Hello, World!

We’re using double quotes to enclose strings. This is because double quotes allow string interpolation

Concatenate Literal Strings and Variables

In our first example we concatenated just variables by placing them one after the other.

When concatenating literal strings and variables we’ll need to make sure the variable doesn’t get interpreted incorrectly when it’s followed by other characters that are valid for a variable name.

For example, if we want to concatenate the variable $a and the literal string xyz, it would be wrong if we simply put them one after the other like this:

a="abc"

result="$axyz"

echo $result

The output will be empty:

The interpreter would just assume that $axyz is a variable. Since the variable $axyz doesn’t exist, echo $result will be empty.

For this we need to set some boundaries so the Bash interpreter knows that $a is a variable. We do this by enclosing it in curly braces ${a}:

a="abc"

result="${a}xyz"

echo $result
Output
abcxyz

Now the Bash interpreter can tell that a is a variable, and separate from the literal string xyz.

Concatenating Variables of Different Types

Bash variables don’t have types, and are essentially all strings. They behave differently depending on context, so you can easily concatenate variables of any “type”.

In the following example a is an integer, that can be used to perform arithmetic operations, and b is a string, and when we just put them one after the other they are concatenated as two strings.

a=1001

b=" Arabian Nights"

echo $a$b
Output
1001 Arabian Nights

Concatenate Strings with the += Operator

Bash also allows string concatenation using the += operator. This is also useful when concatenating strings in loops.

This is a simple example of string concatenation using the += operator.

a="How"
a+=" now"
a+=" brown"
a+=" cow"

echo $a
Output
How now brown cow

This is an example of using the += operator to concatenate strings in a Bash for loop:

a=""

for word in "How" "now" "brown" "cow"; do
    a+="${word} "
done

echo $a
Output
How now brown cow

Conclusion

String concatenation is one of the most common string operations. Hopefully this tutorial helped you understand string concatenation in Bash.

If you encounter any issues or questions please feel free to leave a comment and we’ll get back to you as soon as we can.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Functions
Read More

Bash Functions

The concept of the bash functions is similar to the functions, methods, procedures, or subroutines in the programming…
Bash Compare Strings
Read More

Bash Compare Strings

Similar to other programming languages, strings in bash is the datatype that holds a sequence of characters. In…