concatenate strings in shell script

How to Concatenate String Variables in Shell Script

Sometimes you may need to combine or join strings in shell script. There are several ways to concatenate string variables in shell script. In this article, we will learn how to combine strings in shell. You can use them in terminal shell as well as shell scripts or almost every Linux distribution.


How to Concatenate String Variables in Shell Script

There are several ways to concatenate string variables in shell script.

The simplest way to concatenate shell variables is to just place them next to each other. Here is an example, where we accept user input variables a and b and concatenate them with this method. Let us say you have the following code in shell script test.sh

#Read inputs a and b and store string variables in them.
read a b

#concatenate the strings by writing one after the other. Store the result in c
c=$a$b

#output c
echo 'output:'$c

Now when you run the shell script test.sh

$ ./test.sh
hello 
world
output:helloworld

You can also insert specific text, whitespace characters, hyphens between the two shell variables by enclosing them in double quotes.

c=$a"xyz"$b

Alternatively, you can enclose variable names within curly braces { }. In the following example, we have used shell variables within another string. So we need to enclose them in ${…} for evaluation.

c="${a}-${b}"

Here is an example that uses both these formulas.

#Read inputs a and b and store string variables in them.
read a b

#concatenate the strings by putting one after the other. Store the result in c
c="-"$a" "$b"!"

#output c
echo $c

#alternative solution:
#c="-${a} {b}!"

#output c
echo $c

Here is the output when you run above shell script test.sh.

$ test.sh
hello
world
-hello world!
-hello world!

You can also append one shell variable to another using += operator. Here is an example script test.sh where we concatenate two strings stored in variables a and b.

#Read inputs a and b and store string variables in them.
read a b

#append b to the string a
a+=$b

#Output the resulting string
echo $a

When you run the above script, you will see the following output.

$ test.sh
hello
world
helloworld

You can also use it in a loop to append a set of strings to a given string. Here is an example where we loop through a space separated sequence of strings.

#create an empty string c
c=""

#read input strings and store them in a and b
read a b

#for loop to append a group of strings to the variable c.  
for val in $a 'ipsum' $b 'sit amet' 10
do
 c+="$val "
done

#The result string contains all strings concatenated into a single string
echo "$c"

When you run the above script, you will see the string concatenation as shown.

$ test.sh
lorem
dolor
loremipsordolorit amet10

As you can see in the above example, you can concatenate strings with numbers, without getting errors of mismatching data types.

In this article, we have learnt several examples to concatenate strings in shell script. You can use them in almost every shell. Basically, you can simply place them one after the other for concatenation, or use += operator for appending one string to another.

Also read:

How to Iterate Over Arguments in Shell Script
Bash Script That Accepts Optional Arguments
How to Test If Variable is Number in Shell Script
How to Check if Variable is Number in Python
How to Take Backup of MySQL Database in Python

Leave a Reply

Your email address will not be published. Required fields are marked *