Introduction
Bash utilities make so many tasks simple, but not all tasks have a straightforward command or operator. A very common and usual task in many scripting scenarios is concatenating string variables.
Bash doesn't have a built-in function or command-line utility to concatenate two strings together. However, there are many ways you can accomplish this.
Concatenating Strings in Bash
The following are the steps by which the strings can be concatenated, ordered by the level of complication:
echo
both variables- Format using
printf
- Format using
awk
- Concat string files using
join
Throughout the article, we will be using the variables - string1
and string2
which you can create in your shell by entering:
$ string1="Stuck"
$ string2="Together"
Concatenate Strings in Bash with echo
Perhaps the simplest of all the hacks is to simply echo both variables formatted next to each other:
$ echo "${string1}" "${string2}"
Notice that we have a blank between both the strings. The snippet can also be written as:
$ echo "${string1} ${string2}"
Both of the snippets will give the same output:
Stuck Together
Concatenate Strings in Bash with printf
Let's try concatenating the strings by printing a formatted output on the teletypewriter i.e. terminal. Consider the following snippet, where two string format tags (%s
) are split by an empty space that is ready to be substituted by the two variables - $string1
and $string2
:
$ printf "%s %s\n" "$string1" "$string2"
The two format tags, when substituted with our string variables output:
Stuck Together
String formatting may sound familiar if you are familiar with programming languages. If this sounds overwhelming to you, it's good to learn about what the different tags do (such as %s
and %d
), which could come in quite handy.
Concatenate Strings in Bash with awk
A slightly less known command - the awk
command, invokes AWK - a scripting language specifically designed for manipulating text and extract strings from them. It doesn't come as a surprise that a scripting language dedicated to manipulating strings also has the ability to concatenate them:
$ echo "$string1" "$string2" | awk '{printf "%s~%s\n",$1,$2}'
Let's break up the command above. We have two snippets chained to each other (hence the |
chaining operator). We are sending both the variables string1
and string2
as inputs to the awk
command.
The syntax of the
awk
command is:awk '{some_operation}'
.
In our case, we are performing a printf
operation inside. We refer to the variables as $1
and $2
; these refer to the variables passed as arguments from the echo
.
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Just to show a variation here, we are using a tilde(~
) separating the format tags. Thus, it gives the following output:
Stuck~Together
Concatenate Strings in Bash with join
Now that the bars have been raised, let's try one final method. Let's try storing our strings in two separate files with one column each. The file string1.txt
contains the word "Stuck"
written two times in three lines. Same as before, string2.txt
will contain the word "Together"
written two times in three lines and the following commands create the files for us:
$ echo "1 Stuck" > "string1.txt"
$ echo "2 Stuck" >> "string1.txt"
$ echo "1 Together" > "string2.txt"
$ echo "2 Together" >> "string2.txt"
Both the files can be concatenated by using the following command:
$ join string1.txt string2.txt
For the join command to work, both the files should have a matching column. In our case, we have the matching index column and we get the concatenated output as shown below:
1 Stuck Together
2 Stuck Together
Conclusion
There are multiple ways to perform the concatenation of strings in bash scripting. The choice of the method depends on the level of complexity and the nature of the problem. Which one will you choose?