How to Concatenate Strings in Bash: A Guide for Connecting String Variables
Most programming languages can connect two or more strings. A programming language that makes it easy to concatenate variables is to hit.
What makes bash special is that string variables can be wired 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’ll explain bash scripting, explain what bash concatenation strings are, and provide various ways to concatenate strings.
What is the bash script?
Bash shell scripts allow users to execute hundreds of Linux commands with a single script instead of writing them all one by one. It is especially useful for users looking to automate their physics or virtual private servers (VPS) and increase productivity.
For example, any command that a user can run natively on a terminal can be placed in a bash script. This also applies to functions, so instead of writing them every time, users only need to write a function once and reuse it in any bash script.
Every 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 subsequent lines are the script itself.
In this particular example, the script created a new variable named mybashvariable and gave him a “Hi world” value, and the script will print the value.
What is Concatenation in Linux?
The concatenation operation in bash is to attach a string to the end of another string. Bash allows its users to concatenate strings by writing strings one after another or joining them using the += operator.
String concatenation – Adding one string variable after another
The simplest method of string concatenation is to add one string variable after another. The following sections will show three different ways to do this.
Concatenating strings using string literals
Literal strings are printed literally, and there are two ways to print a literal string – using single quotes or a game symbol with regular double quotes. For example, we’ll create a new unquoted literal string variable 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 Where double quote in the name of the string variable, the echo command will print the value literally:
#!/bin/bash variablename="\usr\bin\env" echo "$variablename"
Here is the result :
# Result \usr\bin\env
Next, we’ll 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 variable using rounded brackets in order to keep it. Braces are useful if you have a variety of variables:
echo "${variablename} Bash_Is_Awesome"
In both cases, the result will be displayed as follows:
\usr\bin\env Bash_Is_Awesome
Concatenation of strings of several variables
Multiple string variables can be easily joined with clear variable manipulation.
For example, in the following bash script, we will use three different variables to create combined values. The echo command will then print the string data:
#!/bin/bash variablename="\usr\bin\env " myvariable="_Awesome" anothervariable="$variablename"Bash_Is"$myvariable" echo "$anothervariable"
This is what the result will look like:
\usr\bin\env Bash_Is_Awesome
Concatenation of strings of numbers and strings
Bash allows its users to concatenate one or more variables which are not of type string. 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
Concatenating strings using the += operator
Another way to join two or more strings to create a concatenated string is to use the addition assignment operator (+=). This operator connects strings using one or more variables.
For example, the following script can be used to join two strings using 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 obtained 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
Concatenation of 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 want to add the numbers, this logic should 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 to use the bash concatenation feature is to implement it in the bash for loop.
In the following example, we got a my variable with three strings and a named variable results with an empty string. With the help of hit for loopwe will be able to combine strings of my variable with our channel:
#!/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 went over the definition of bash scripts and concatenation. We also learned how to join string variables with two different methods.