In bash scripting, we can add or join two or more strings which are called string concatenation. For example, two strings having “Linux ” and “Ubuntu” can be concatenated to form a single string as “Linux Ubuntu” or “Ubuntu Linux”.
The bash script does not have any built-in function to perform string concatenation, but various methods can be utilized to perform string concatenation.
This article will discuss the possible ways to concatenate the string with the below timeline:
- How to Concatenate Bash Strings?
- Example 1: Concatenate Two Bash Strings
- Example 2: Concatenate Strings Taking User Input
- Example 3: Concatenate Strings Using the Curly Braces {}
- Example 4: Concatenate Strings Using the +=
- Example 5: Concatenate Multiple Bash Strings
- Example 6: Concatenate Strings Using for Loop
- Additional Tip: Concatenate Two Numbers
How to Concatenate Bash Strings?
There are several ways to concatenate strings; let’s understand the concatenating strings methods with the help of examples:
Example 1: Concatenate Two Bash Strings
To concatenate two strings, we can simply write them together. For instance, two strings variables are created, “Str1” and “Str2”, which will be concatenated in the below bash script:
Note: The strings can be concatenated with (as done in the below script) or without space to add blank space between two strings.
#!/bin/bash
Str1="Linux,"
Str2="Ubuntu"
echo "$Str1 $Str2"
Run the bash script file using the below command:
$ bash concat.sh
The output shows that both strings are concatenated.
Example 2: Concatenate Strings Taking User Input
To concatenate two strings with the user input, we can use the below script:
#!/bin/bash
read -p "Enter your first string: " Str1
read -p "Enter your second string: " Str2
echo "Your string is: $Str1 $Str2"
Let’s run the script, with the below bash command:
$ bash concat.sh
The output shows two strings are entered by the user “Linux” and “Ubuntu”, which is concatenated as “Linux Ubuntu”,
Example 3: Concatenate Strings Using the Curly Braces {}
The curly braces {}, the format is used to concatenate the strings. To concatenate two strings variables Str1 “Linux, ” and Str2 “Ubuntu”, the below script is used:
#!/bin/bash
Str1="Linux, " Str2="Ubuntu"
echo "${Str1}${Str2}"
To check the output of the above script, use the following command:
$ bash concat.sh
The output shows that both strings are concatenated as “Linux, Ubuntu”.
Example 4: Concatenate Strings Using the +=
You can concatenate two strings using the “+=” operator. For instance, we have used a single variable Str1 “Linux, ” which can be concatenated with another string “Ubuntu”, using the += operator with the below bash script:
#!/bin/bash
Str1="Linux, "
Str1+="Ubuntu"
echo "$Str1"
To run the script, use this command:
$ bash concat.sh
Two strings are concatenated, and the output can be seen as “Linux, Ubuntu”.
Example 5: Concatenate Multiple Bash Strings
Several strings can be concatenated by using the names together. For instance, to concatenate four strings, the below script can be utilized:
#!/bin/bash
Str1="Linux, "
Str2="Debian, "
Str3="RHEL, "
Str4="Fedora"
echo "$Str1$Str2$Str3$Str4"
To run the above bash script, use the following command:
$ bash concat.sh
The four strings are concatenated, as seen in the output.
Example 6: Concatenate Strings Using for Loop
We can concatenate the string using the for loop. To concatenate a number from a loop with a string, use this script:
#!/bin/bash
Numbers="1 3 5"
Result=""
for Num in $Numbers
do
Result+="The Number is: ${Num}. "
done
echo $Result
Let’s understand the script line by line:
- Numbers: We declare a Variable having numbers, and every number will be concatenated with the string.
- Result: Will store the concatenated string.
- for: The for loop will run every time, and a number will be concatenated with the string.
- Result+= : It adds the number with the string.
Let’s run the above shell script using the following command:
$ bash concat.sh
“The number is: ” text is concatenated with an element of Numbers when the for loop runs.
Additional Tip: Concatenate Two Numbers
Two numbers can also be concatenated; when we concatenate two numbers, it does not add the number rather, it just appends the number to the first number. For instance, to concatenate two numbers, “Num1” and “Num2”, the below shell script can be used:
#!/bin/bash
#!/bin/bash Num1=15 Num2=12 Num3="$Num1$Num2" echo "$Num3"
To execute the script the following bash command is utilized:
$ bash concat.sh
The output shows the two numbers are concatenated (12 is appended with 15).
Conclusion
Several methods are used to concatenate the strings, such as writing the strings together ($Str1$Str2), using curly braces (${Str1}${Str2}), and using the += operator (Str1+=”$Str2”). Moreover, we can concatenate the string using the for loop. This post has briefly explained the various methods to concatenate strings in bash.