Updated April 14, 2023
Introduction to Bash Concatenate strings
The following article provides an outline for Bash Concatenate Strings. For strings specifically, it means joining of character or strings end to end. On a literal sense, it means merging 2 things together. In any programming language, concatenation is a common requirement everywhere. Now when we saw about other programming languages which are rather latest in terms of development has a specific character or may be an in-built function which performs the task of concatenation. But when it comes to bash there is no built-in function or specific character which accomplishes this task and is rather much simpler than we anticipate it to be and that is by placing the strings next to each other or by usage of shorthand operator.
Let us see the usage of concatenation in real life use cases. In cases of printing out some variables entered by user along with some constant string which needs to be printed out needs to go through concatenation of strings to make a meaningful sentence. The next use case may be in terms or making a group out of 2 or more elements to identify a collection of elements under one umbrella. Apart from these 2, there are many other uses of concatenation one would need to do in bash to fulfill the problem statement goals.
Methods of Bash Concatenate Strings
Given below are the methods of Bash Concatenate strings:
One can easily weigh each method to find the best one in solving a particular problem statement.
1. Side by side merging of strings
In this method, we would put the different strings just side by side of each other, and this will enable us to achieve the required concatenation of strings. This method is the simplest method out of everything else in this list.
The syntax of the method goes as:
For example, we have 2 variables, var_1 and var_2, and to concatenate we would place the variables as: $var_1$var_2 and the necessary task will be accomplished.
2. Usage of double quotes
This method is again widely used when we need to concatenate a constant string along with a variable passed on by user. For this we take help of double quotes to accomplish the task. Both the string along with the variable name is included under the double quotes to accomplish the task.
The syntax goes as:
For example we would need to print a website name “educba.com” entered by user, along with string we would use var_join=“The website you entered is $1” where $1 is the input entered as an argument when the shell script is run.
3. Using of append operator
In bash we do have a specific operator for appending and that is denoted as “+=”. This approach is used for getting a mathematical sense or may be for some, a sense of newer programming language taste in bash itself. Here the first part is stored in a variable and then we use the += operator to add the other part of the column into the first one. This particular methodology has an application if we want to keep replacing the old variable value without having to make a new variable for every concatenation and mostly used in a for loop, when we have to concatenate element onto the previous one and keep repeating for the next.
The syntax goes as:
var_1="This is the first variable."
var_1+=" Added another line to the var_1"
The var_1 variable will get updated post the second step execution.
4. Printf function
This methodology is not much widely used in the industry and the usage is very limited to special use cases.
The syntax for the same goes as:
printf -v c "The website you entered is $1"
where $1 is the input entered as an argument when the shell script is run.
Example of Bash Concatenate Strings
Given below is the example of Bash Concatenate strings:
Here we have taken all the four methodologies so that it becomes evident on how different things are getting used in a face to face comparison.
Code:
#!/bin/bash
echo "**Example of String concatenation by placing side by side**"
var_1="Welcome to the article of "
var2="String Concatenation"
echo "By putting var_1 and var_2 side by side and we get:"
echo $var_1$var_2
echo "**Example of String concatenation by placing side by use of double quotes**"
echo "Input by user as name of website is: $1"
var_3="The website you have reached is: https://www.$1.com"
echo $var_3
echo "**Example of String concatenation by placing side by using +=**"
var_4="Now we will use += to add the next line."
var_4+=" The next line got added along with the previous one!"
echo $var_4
echo "**Example of String concatenation by using printf**"
printf -v printf_variable "You can access more articles on bash and shell script at https://www.$1.com"
echo $printf_variable
Output:
Conclusion
Hence, in this article we saw overall insight on usage of different methodologies used in the professional world and have provided an even sense of usage of each of them in solving a near to real life problem statement. As the next step we would encourage you to try these hands on without looking at the syntax and also encourage you to start using each of them in your solution of solving a use case.
Recommended Articles
We hope that this EDUCBA information on “Bash Concatenate Strings” was beneficial to you. You can view EDUCBA’s recommended articles for more information.