Concatenation is one of the most commonly used string operations in Bash scripting. It is used to join one string to another and creating a new string.
String Concatenation is a common requirement of any programming language. By default, Bash does not contain any function to combine string data. The easiest way to concatenate strings in Bash is to write variables side by side.
In this tutorial, we will learn how to concatenate strings in Bash Shell Scripting.
Concatenate Strings
To understand this concept, assume that we have two strings (“Welcome” and “to Webservertalk”), and we combine both the strings together and create a new string “Welcome to Webservertalk”.
This is called String Concatenation.
In the following example, we are concatenating two or more strings variable one after one for concatenating them together.
#!/bin/bash
a="Welcome"
b=" to Webservertalk"
c="$a$b"
echo "$c"
After running the above script you should see the following output:
Welcome to Webservertalk
You can also concatenating strings with the number as the value shown in the following example:
#!/bin/bash
a="Welcome"
b=" 2"
c=" Worlds"
d="$a$b$c"
echo "$d"
After running the above script you should see the following output:
Welcome 2 Worlds
Use a string variable in another String
You can add the string variable in another string in any position of the string data.
Let’s see the following example:
#!/bin/bash
a="Learn"
b="$a Bash Scripting"
echo "$b"
After running the above script you should see the following output:
Learn Bash Scripting
You can also add the first string variable in the middle of the other string:
#!/bin/bash
a="Programming"
b="Learn $a Language"
echo "$b"
After running the above script you should see the following output:
Learn Programming Language
Combine String with the += Operator
You can also concatenate strings by using the += operator. It is one of the easiest way to concatenate strings.
Let’s see the following example:
#!/bin/bash
a="Programming"
a+=" Language"
echo "$a"
After running the above script you should see the following output:
Programming Language
You can also use the += operator to concatenate strings inside a “for” loop.
Let’s see the following example:
#!/bin/bash
echo "See the list of fruits"
fruits=""
for name in 'Banana' 'Apple' 'Mango' 'Pineapple'; do
fruits+="$name "
done
echo "$fruits"
After running the above script you should see the following output:
See the list of fruits
Banana Apple Mango Pineapple
Concatenate Strings with literal Strings
The literal string variable can be used to concatenate with other string data.
In the following example, we will use $a variable to store string data. Then, it is used as a literal string in $b variable.
#!/bin/bash
a="Learn programming"
b="${a} from the basics"
echo "$b"
After running the above script you should see the following output:
Learn programming from the basics
Conclusion
In the above tutorial, we’ve gone over all possible ways of concatenating strings in bash with several examples. Concatenating string variables is very useful in Bash scripting to generate meaningful output. I hope you can now able to combine strings properly in the bash script.