Sep 20, 2021 3 min read

How to Concatenate Strings in Bash

Learn to concatenate strings in bash shell scripts. You'll also learn to append to existing strings and combine strings and integers.
Table of Contents

Concatenating strings can be an important part of using any programming language for practical applications.

You can concatenate strings in bash as well. There is no concatenation operator here. Just write the strings one after another to join strings in Bash.


Don't worry! I’ll show you various actual examples to concatenate strings in bash.

Assigning concatenated strings

There are no data types in Bash like you have in most programming languages. But you can still declare variables in Bash.

Here is how you assign strings in Bash:

[email protected]:~$ w='Welcome'

You can use printf command to print the value of this string variable:

[email protected]:~$ printf "$w\n"

Let's create some more strings:

[email protected]:~$ t='To'
[email protected]:~$ l='Linux'
[email protected]:~$ h='Handbook!'

I want to combine all these string variables into a single one. How to do that?

[email protected]:~$ tony="${w} ${t} ${l} ${h}"

In this manner, I have concatenated all four strings into a single variable and named it tony. Do note that I have added a space between the variables.

Let's quickly confirm that the strings have been combined:

[email protected]:~$ printf "$tony\n"
Welcome To Linux Handbook!

Here's all of it in a Bash Script:

tony="${w} ${t} ${l} ${h}"
printf "${tony}\n"

Make it executable and run it as a script:

[email protected]:~$ chmod +x
[email protected]:~$ ./
Welcome To Linux Handbook!
The curly braces {} around the variable names are not mandatory while concatenating strings. However, to make things clear and protect it from surrounding characters, it is good practice to wrap them in {}.

Append to string in bash

The above example combines different strings into one.

Let's take another scenario. Say, you want to append to an already existing string. How to do that? You use the wonderful += operator.


Can you guess the new value of str? Yes! It is ironman.

[email protected]:~$ str="iron"
[email protected]:~$ str+="man"
[email protected]:~$ echo $str

This is helpful when you are using loops in bash. Take this for loop for example:


for color in 'Black' 'White' 'Brown' 'Yellow'; do
  var+="${color} "

echo "$var"

If you run the above script, it will append to the string after each iteration.

Black White Brown Yellow

Concatenate numbers and strings

As I mentioned previously, there are no data types in Bash. Strings and integers are the same and hence they can be easily joined in a single string.

Let us look at another example through a second script. This time, I'll use a number:

stark="${we} ${lv} ${y} ${morgan}!!!"
printf "${stark}\n"


[email protected]:~$ chmod +x
[email protected]:~$ ./
We Love You 3000!!!

Nested concatenation of strings

You can also store those two concatenated strings inside a third one through nested concatenation:

tony="${w} ${t} ${l} ${h}"
stark="${we} ${lv} ${y} ${morgan}!!!"
printf "${ironman} Forever!\n"

When you run this shell script, you'll see this output:

Welcome To Linux Handbook..We Love You 3000!!! Forever!


If you are new to shell scripting, I highly recommend our Bash tutorial series for beginners.

Bash Tutorials for Beginners: Start Learning Bash Scripting
Here’s a collection of bash tutorials that will teach you bash shell scripting from the beginning. You’ll learn all the basics of bash scripting.

I hope this quick little tutorial helped you in concatenating bash strings. If you have questions or suggestions, feel free to leave a comment below.

Join the conversation

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.