In this article, we will see the concatenation of string in bash scripting.
Joining two or more strings together is called string concatenation. Bash does not have any built-in function to perform the concatenation on string data or variables. There are various methods using which we can perform the concatenation of strings in bash, those are :
Method 1: Write variables side by side
This is the easiest method to perform concatenation.
Example: Let’s take two strings (i.e., “welcome” and “to geeksforgeeks”), and we want to return a new string which is the combination of the given two strings.
Code:
#!/bin/bash
# Script to Concatenate Strings
# Declaration of first String
str1="Welcome"
# Declaration of Second String
str2=" to GeeksforGeeks."
# Combining first and second string
str3="$str1$str2"
# print the concatenated string
echo $str3
Output:
Welcome to GeeksforGeeks.
Method 2: Using Double Quotes
It is also one of the easy methods to perform concatenation. This method uses variables inside the string, which is defined with double quotes. The advantage of using this method is we can concatenate the string variable at any position of the string data.
Example: Let’s concatenate two strings (i.e., “to” and “Welcome geeksforgeeks”) in such a way that it returns the result as “Welcome to GeeksforGeeks”.
Code:
#!/bin/bash
# Concatenate Strings
# Declaration of String Variable
str="to"
# Add the variable within the string
echo "Welcome $str GeeksforGeeks."
Output:
Welcome to GeeksforGeeks.
Method 3: Using printf function
printf is a function in bash that is used to print and concatenate the strings.
Syntax:
printf -v new_str “$str string_to_concatenate.”
This command will concatenate the data present inside the double quotes and store the new string into the new_str variable. In this method also we can concatenate the string variable at any position.
Example: Let’s concatenate two strings (i.e., “to” and “Welcome geeksforgeeks”) in such a way that it returns the result as “Welcome to GeeksforGeeks”.
Code:
#!/bin/bash
str="to"
printf -v new_str "Welcome $str GeeksforGeeks."
echo $new_str
Output:
Welcome to GeeksforGeeks.
Method 4: Using Literal String
In this method, concatenation is performed with a literal string by using curly braces{}. It should be used in such a way that the variable does not mix up with the literal string.
Let’s concatenate two strings (i.e., “to” and “Welcome geeksforgeeks”) in such a way that it returns the result as “Welcome to GeeksforGeeks”.
Code:
#!/bin/bash
str="to"
# concatenation of strings
new="Welcome ${str} GeeksforGeeks."
echo "$new"
Output:
Welcome to GeeksforGeeks.
Method 5: Using Loop
This method is used when we have to concatenate strings present inside the list.
Syntax:
newstr=" "
for value in list;
do
# Combining the list values using append operator
Newstr+="$value "
done
Example
Code:
lang=""
# for loop for reading the list
for value in 'Welcome ''to ''GeeksforGeeks''!!';
do
# Combining the list values using append operator
lang+="$value "
done
# Printing the combined values
echo "$lang"
Output:
Welcome to GeeksforGeeks!!
Method 6: Using Any Character
If we want to concatenate strings separated by certain characters, we use this method. This is similar to writing variables side by side.
In this method, we write variables side by side with the character in between.
Example: Let’s concatenate strings(‘Apple’, ‘Mango’, ‘Guava’, ‘Orange’) separated by comma(,) character.
Code:
str1="Apple"
str2="Mango"
str3="Guava"
str4="Orange"
# concatenate string using ','
echo "$str1,$str2,$str3,$str4"
Output:
Apple,Mango,Guava,Orange
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Jan, 2022
Like Article
Save Article