Thursday, February 18, 2016

Concatenation of two or more strings in Bash

String concatenation is one of the basic requirements expected from any language. Bash provides various ways to do it. Let us see some of them here.

The most intuitive way to do string concatenation is by writing the strings next to each other.
For example, if the two strings are stored in a variable:
var1="Hello"
var2="World"
var3=$var1$var2
echo $var3
This would print the value of $var3 as:
HelloWorld
Note the lack of space between the two strings that are concatenated.

If you want to print it with space in between, then it has to be changed to:
var1="Hello"
var2="World"
var3="$var1 $var2"
echo $var3
In this case, you need to enclose the RHS of the assignment to var3 in double quote or store it in another variable.

A slightly different way of doing it is by using double quotes around the space alone:
var1="Hello"
var2="World"
var3=$var1" "$var2
echo $var3
If you watch carefully, that shows some interesting behavior. You can concatenate a string stored in a variable and a string literal by just placing one next to the other. This is useful when we have a file name and we want to add something else to name say .bak to say that it is a backup file. Let us try that:
filename="list_of_files.txt"
bak_filename=$filename".bak"
echo $bak_filename
This will give the output:
list_of_files.txt.bak
In fact, if bak is stored in a variable, then the quotes around the dot(".") is also optional:
filename="list_of_files.txt"
bak_extn="bak"
bak_filename=$filename.$bak_extn
echo $bak_filename
This will also give the same output:
list_of_files.txt.bak
But it will not work in all the cases. For example:
filename="list_of_files.txt"
bak_extn="bak"
bak_filename=$filename_$bak_extn
echo $bak_filename
This will give the output:
bak
To explain, only the value stored in the second variable was printed. The first variable was ignored. Why?
The culprit is the underscore("_"). Underscore is allowed as a special character in a variable name. So, instead of interpreting $filename as the variable name, the shell will interpret $filename_ as the variable name.
One way to do this is as follows:
var1="file_list"
var2="bak"
var3=$var1"_"$var2
echo $var3
var4=$var1\_$var2
In this case, the backslash ("\") specifies that the underscore is not a part of the previous variable. Another way to do this is as follows:
var1="file_list"
var2="bak"
var3=${var1}"_"${var2}
echo $var3
var4=${var1}_${var2}
This gives the output:
file_list_bak
file_list_bak
Of course, as usual, the double quotes are optional. There are people who find the usage of double quotes in for concatenation disturbing. They can use the curly braces instead.

Finally, let us see a special case. If you want to append a string to a string that is already stored in a variable, then use this method:
var1="Hello "
var2="World"
var1+=$var2
echo $var1
var1="Hello "
var1+="World"
echo $var1
This gives the output:
Hello World
Hello World
The operator += appends the string to the RHS of the operator to the variable on the LHS. So, in this case the value of the variable on the LHS will change.

Post a Comment