In this article, we will discover 5 easy hacks to concatenate string using Linux Bash shell scripting.
Using Variable Substitution
The concatenation of string using variable substitution is the easiest method to join two string. You need to take two bash variables and place both of them inside single quotes and assign it to a variable.
# 1) Using Variable Substitution First_string="Python" Second_string="Baba" String_concatenation="$First_string$Second_string" echo "$String_concatenation"
By appending to a string using += operator
In this method, you can concatenate two strings using += operator and assign it to the second variable.
# 2) Appending to a string using += operator First_string="Python" Second_string="Baba" Second_string+="$First_string" echo "$Second_string"
Concatenate by Prepending to a String
Here you can prepend the first string before the second string inside the double quotes using $ sign and assign it to the second string variable.
# 3) Prepending to a string First_string="Python" Second_string="Baba" Second_string="$First_string$Second_string" echo "$Second_string"
Concatenate two strings using printf
This is a savvy technique where you print two string together using printf command and assigning it to a variable.
# 4) String concatenation using printf First_string="Python" Second_string="Baba" String_concatenated="$(printf "%s%s" "$First_string" "$Second_string")" echo "$String_concatenated"
Concatenate two strings using for Loop
You can execute a for loop equal to the number of characters in both the string and reading the character with each pass and assigning it to the concatenated string variable.
# 5) Using For loop
First_string="Python"
Second_string="Baba"
Final_string="$(for C in First_string Second_string; do echo -n "${!C}"; done)"
echo "$Final_string"
Concatenate two strings using Command Separator
Print the two string using command separator with the help of echo command then assigning it to the final concatenated string variable.
# 6) Using Command Separator First_string="Python" Second_string="Baba" Final_string="$(echo -n "$First_string"; echo -n "$Second_string")" echo "$Final_string"
Concatenate two strings using Using File Method
It is one of a tricky method, where you can redirect the content of the first string to a temporary file. Then appending the content of second-string using >> operator to the temporary file. Finally, reading the content of the temporary file and assigning it to the final concatenated string variable.
# 7) Using temporary file First_string="Python" Second_string="Baba" echo -n "$First_string" > tmp echo -n "$Second_string" >> tmp read Final_string < tmp echo $Final_string rm tmp
Concatenate two strings using Process Substitution
Here we will substitute the output of first and second string as the input for the cat command process and assigning it to the final concatenated string variable.
# 8) Using Process substitution First_string="Python" Second_string="Baba" Final_string="$(cat <(echo -n "$First_string") <(echo -n "$Second_string"))" echo "$Final_string"
So, we discussed eight different ways to concatenate string using Linux Bash shell scripting. If you like to add more do comment. Thanks for reading.