Open In App

Bash Concatenate String

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Bash scripting, a powerful and versatile tool in the Linux world, allows users to automate tasks and streamline processes. String manipulation is a fundamental aspect of scripting, and one common operation is concatenating strings. Concatenation involves combining multiple strings into a single string, facilitating the creation of dynamic and customized outputs in scripts. In this article, we’ll delve into the intricacies of string concatenation in Bash, exploring various methods and providing illustrative examples.

1. Write variables side by side for Bash Concatenate String

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. 

We can use `str3 = “$str1$str2″`

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:

Here we first created the script name “side.sh” using Vim editor, then made our script executable using `chmod +x` command and finally run our script.

side by side Concatenate String

side by side Concatenate String

This method directly concatenates two strings by placing them adjacent to each other, forming the desired output.

2. Using Double Quotes for Concatenate String

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:

Here we first created the script name “double.sh” using Vim editor, then made our script executable using `chmod +x` command and finally run our script.

Double Quotes for Concatenate String

Double Quotes for Concatenate String

Using double quotes enables the insertion of variables at any position within the string, enhancing customization.

3. Using printf function for Concatenate String

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:

Here we first created the script name “printf.sh” using Vim editor, then made our script executable using `chmod +x` command and finally run our script.

printf for Concatenate String

printf for Concatenate String

This method provides control over string formatting and concatenation, offering an alternative to direct variable assignment.

4. Using Literal String for Concatenate 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:

Here we first created the script name “literal.sh” using Vim editor, then made our script executable using `chmod +x` command and finally run our script.

Using Literal  for  Concatenate String

Using Literal for Concatenate String

Using curly braces ensures the variable is distinct from the literal string, avoiding ambiguity.

5. Using Loop for Concatenate String

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:

#!/bin/bash

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:

Here we first created the script name “loop.sh” using Vim editor, then made our script executable using `chmod +x` command and finally run our script.

Using Loop to Concatenate String

Using Loop to Concatenate String

Looping through a list allows dynamic concatenation, creating a cohesive output.

6. Using Any Character for Concatenate String

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:

#!/bin/bash

str1="Apple"
str2="Mango"
str3="Guava"
str4="Orange"

# concatenate string using ','
echo "$str1,$str2,$str3,$str4"



Output:

Here we first created the script name “characters.sh” using Vim editor, then made our script executable using `chmod +x` command and finally run our script.

Any character for Concatenate String

Any character for Concatenate String

Here, strings are concatenated with a comma between them, showcasing the flexibility of this method.

Conclsuion

In this article we discussed mastering string concatenation in Bashwhich opens doors to crafting dynamic and customized outputs in scripts. Each method offers distinct advantages, providing scriptwriters with a toolkit to tailor concatenation based on specific requirements. Whether writing variables side by side, utilizing double quotes, employing the printf function, working with literal strings, utilizing loops, or incorporating specific characters, understanding these methods enhances the efficiency of Bash scripting endeavors.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads