How to Concatenate Strings in Bash: A Guide for Connecting String Variables
The majority of programming languages can connect two or more strings. One programming language that makes it effortless to concatenate variables is bash.
What makes bash special is that string variables can be connected without the use of dedicated commands or functions. In other words, to combine string data, users can use simple variable manipulation or apply the same logic with the addition assignment operator (+=).
In this tutorial, we will explain bash scripting, go over what bash concatenate strings are, and provide a variety of ways to concatenate strings.
What Is Bash Scripting?
Bash shell scripting allows users to execute hundreds of Linux commands with a single script instead of writing them all down one by one. It is extra helpful for users looking to automate operations on their physical server or VPS hosting environment, enhancing productivity.
For example, any command a user can run natively on a terminal can be put into a bash script. This also applies to functions, so instead of writing them down every time, users only need to write a function once and reuse it in any bash scripts.
Any script starts with a .sh file and contains a similar structure:
#!/bin/bash # Creates a new variable "Hello, World" mybashvariable="Hello, World" echo $mybashvariable
The first line tells the terminal to run the script using bash exclusively, and all the following lines are the actual script itself.
In this particular example, the script created a new variable named mybashvariable and gave it a “Hello, World” value, and the script will print the value out.
What Is Concatenation in Linux?
Concatenation operation in bash is the process of attaching a string to the end of another string. Bash allows its users to concatenate strings by writing strings one after the other or joining them using the += operator.
String Concatenation – Adding One String Variable After the Other
The simplest string concatenation method is adding one string variable after the other. The following sections will show three different ways to do just that.
String Concatenation Using Literal Strings
Literal strings are printed literally, and there are two ways to print out a string literal – using singular quotes or a backlash symbol with regular double quotes. For example, we will create a new literal string variable without quotes and echo it:
#!/bin/bash variablename=\usr\bin\env echo "$variablename"
In this case, the result would be:
# Result usrbinenv
Now, when we add singular or double quotes to the string variable name, the echo command will print the value literally:
#!/bin/bash variablename='\usr\bin\env' echo "$variablename"
Here’s the result:
# Result \usr\bin\env
Next, we will apply this logic to concatenate two strings:
#!/bin/bash variablename='\usr\bin\env' echo "$variablename Bash_Is_Awesome"
We can also cover the last line’s variable using rounded brackets in order to guard it. Curly brackets are helpful if you got a variety of variables:
echo "${variablename} Bash_Is_Awesome"
In both cases, the result will be shown as:
\usr\bin\env Bash_Is_Awesome
String Concatenation of Multiple Variables
Multiple string variables can be easily joined together with clear-cut variable manipulation.
For example, in the following bash script, we will use three different variables to create combined values. The echo command will subsequently print out the string data:
#!/bin/bash variablename='\usr\bin\env ' myvariable='_Awesome' anothervariable="$variablename"Bash_Is"$myvariable" echo "$anothervariable"
Here’s what the result will look like:
\usr\bin\env Bash_Is_Awesome
String Concatenation of Numbers and Strings
Bash allows its users to concatenate one or more variables that are not string-type. For this reason, it is possible to concatenate multiple variables, which can be strings or numbers:
#!/bin/bash firstvariable=" Hello, World " secondvariable="Hello, Hostinger " thirdvariable=" I now know how to concatenate strings in bash." fourthvariable="$secondvariable"and"$firstvariable"means"$thirdvariable" echo $fourthvariable
String Concatenation Using the += Operator
Another way to join two or more strings to make a concatenated string is to use the addition assignment operator (+=). This operator makes it possible to connect strings using one or more variables.
For example, the following script can be used to join two strings with the use of a single variable:
#!/bin/bash mystring="I would like to generate a meaningful output, please. " mystring+="Not a chance, friend!" echo "$mystring"
A similar result can be achieved using two variables:
#!/bin/bash firststring="This is a single string. " secondstring="Which makes this a resulting string." # Curly brackets between $secondvariable are called variable interpolation. firststring+="${secondstring}" echo $firststring
Concatenating Numeric Strings
The append operator method can also be used exclusively to append numeric string variables.
#!/bin/bash numeric_string=2050 numeric_string+=0502 echo $numeric_string
However, if you would like to add the numbers together, this logic needs to be used:
#!/bin/bash x=3 y=5 z=6 ((x+=y+=z)) echo $x
Concatenating Strings Using Bash for Loop
A more advanced way of using the bash concatenate functionality is implementing it into the bash for loop.
In the following example, we got a myvariable with three strings and a variable named results with an empty string. With the help of the bash for loop, we will be able to combine strings from myvariable with our string:
#!/bin/bash myvariable="bash concatenation Hostinger" results="" for i in $myvariable do results+="The answer is $i... " done echo $results
Conclusion
The bash programming language is a convenient and efficient tool for various variable manipulation actions. One of the most important examples is the ability to join different string variables into one.
In this tutorial, we’ve gone through the definition of bash scripting and concatenation. We also learned how to join string variables with two different methods.
We hope you found this tutorial useful. If you have any further questions, feel free to leave them in the comments section below.