07-22-2023, 04:51 AM
How to Concatenate Strings in Bash: A Guide for Connecting String Variables
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


