Bash concatenate strings in text processing

31
linux

In text processing, joining two or more strings together is call concatenation. It is a normal function on almost programming languages today. But, there is no built-in function to do the concatenation operation in bash shell. Then, we need to find a solution to do this operation in bash. String data can be combined easily by placing each variable in the same line assigned into a new variable. So in this tutorial, we will show you some examples about how to concatenate strings in bash shell.

String concatenation in bash

Example 1: Using string variable after another

The easiest way to concatenate string in bash shell is to place a string after another. Let’s create a file called example1.sh with the following code:

#!/bin/bash
#first string
string1="Welcome"
#second string
string2=" everyone "
#Combine first and second strings
string3=$string1$string2
# Print the third string
echo "$string3 to VSUDO"

In the above code, there are three variables are $string1, $string2 and $string3 variable has value is $string1 and $string2 string values. Then, the value of $string3 is printed we will get this output:

Welcome everyone to VSUDO

Example 2: Combination string by using shorthand ‘+=’ operator

The second way to concatenate strings is using shorthand ‘+=’ operator in bash shell. Let’s create the second script is call example2.sh with the following code:

#!/bin/bash
echo "Printing the list of foods"
#Initialize the variable
foods=""

#for loop for reading the list
for value in 'ice-cream' 'Noodle' 'Pizza' 'Burger'; do

#Combine the list values by using shorthand operator
foods+="$value "
done

#Print the combined values
echo "$foods"

You can see, the shorthand operator is used to concatenate each element of a food list inside loop for. The first $foods variable is initialized with an empty data. Then we use for loop to read data from a food list, each value of the list will be concatenated with the other serially with a white space in each iteration of the loop. Finally, we print $foods variable after the loop and when we excute this script, we will get the output like this:

Printing the list of foods
ice-cream Noodle Pizza Burger

Example 3: Using literal strings

In bash, we can use literal strings to concatenate strings. Let’s create a file called example3.sh and add the following code:

#!/bin/bash
#Declare a string variable
string="How to concatenate strings in bash shell"
#Print the string variable as literal
echo "${string} from the basics"

Look at the above code, $string variable is assigned a string data and then it is used as a literal string in echo command to print final result. If we excute the above code, we will get the output like this:

How to concatenate strings in bash shell from the basics

Conclusion

In bash shell, string concatenation forms a new string that is the combination of multiple strings. There are many ways to concat string in bash. In this tutorial I just show you some ways to do it simply and easily. All posible way to combine multiple strings are exlained clearly in this tutorial. If you have any question about this, don’t be afraid to contact me to get more information.

Previous articleBash split string in text processing
Next articleHow to use bash sleep command in Linux?