How to Concatenate String Variables in Bash [Join Strings]

Two or more string variables of characters or numbers or a mix of characters and numbers can be concatenated easily in bash. The string variables are required to concatenate for different types of programming tasks. Many ways exist in bash to concatenate string variables.

In this tutorial, we learn different ways of concatenating string variables in bash.

What is String Concatenation?

The way of generating new strings by combining string values one after another is called string concatenation. String concatenation can be done by using two or more string variables with a string literal or without any string literal in bash.

1. Concatenating variables with string literals

The string variables and string literal can be concatenated by using a double quotation in the `echo` command. The following script will concatenate two string variables with the string literal.

#!/bin/bash#Declare a string variablestrvar1="Bash"#Declare another string variablestrvar2="Script"#Print the concatenated stringecho "Learn $strvar1 $strvar2"

concatenate string literals

Using the script we have concatenated the two string variables with value ‘Bash’ and ‘Script’ to print the results as “Learn Bash Script”. Basically, we placed the two variable values into a sentence.

2. Concatenating variables of string and number

The numeric value is converted into a string value when concatenated with a string value. The following script will concatenate a string variable with a number variable. The `echo` command has been used to print the concatenated string as in the previous example.

#!/bin/bash#Declare a string variable of multiple wordsitem_name="Logitech Keyboard"#Declare a number variableitem_code=1078#Print the concatenated stringecho "The code of $item_name is $item_code"

concatenate string with number

In the script we have concatenated one string variable with the value, 'Logitech Keyboard' and the number variable with the value, 1078.

3. Concatenating variables without any separator

The following script will concatenate three string variables without any separator. The `echo` command has been used to print the concatenated string like in the previous example.

#!/bin/bash#Declare three string variablesstrvar1="Tic-"strvar2="Tac-"strvar3="Toe"#Print the concatenated stringecho "$strvar1$strvar2$strvar3"

Concatenate string without any separator

Here we have concatenated three string variables with the values, 'Tic-', 'Tac-', and 'Toe'.

5. Concatenating variables with a separator

The following script will concatenate three string variables with a separator. The colon (:) has been used as a separator in the script. The `echo` command has been used to print the concatenated string as in the previous example.

#!/bin/bash#Declare three string variablesstrvar1="Linux"strvar2="Windows"strvar3="Mac"#Declare a string with a separator valueseparator=":"#Print the concatenated stringecho "$strvar1 $separator $strvar2 $separator $strvar3"

Concatenate string with separator

Using the script we have concatenated three string variables with the values, 'Linux', 'Windows', and 'Mac' by adding a separator, ':'.

6. Concatenating variables with shorthand operator

The following script will take three input values from the user and concatenate three values with another string by using a shorthand operator (+=). The values of the string variables will be added at the end of the content of the string variable that has been used on the left side of the operator. The `echo` command has been used to print the concatenated string as in the previous example.

#!/bin/bash#Declare a string variableoutput="Selected languages are:"#Print a prompt messageecho "Type three programming languages:"#Take three input valuesread lang1 lang2 lang3#Concatenate the string valuesoutput+="$lang1, $lang2,and $lang3"#Print the concatenated stringecho -e $output

Concatenate string using operator (+=)

In the script we have concatenated three string variables with the values, 'Bash', 'Python', and 'Perl' by using the shorthand operator, '+='.

7. Concatenating variables with a newline

You can use newline(\n) as a separator to concatenate string variables in bash. The following script will concatenate two string variables by using newline(\n) and the value of each variable will be printed in each line. The `echo` command has been used to print the concatenated string as in the previous example.

#!/bin/bash#Declare two string variablesid="07856"applicant_name="Nehal Sharif"echo "The concatenated values are:"#Print the concatenated stringecho -e "$id\n$applicant_name"

Concatenate string with newline

You can see we have concatenated two string variables with the values, '07856', and 'Nehal Sharif' by adding newline(\n).

8. Concatenating variables with a special character

The following script will concatenate two string variables with some literal string values and a special character, ‘$’. The backslash (‘\’) has been used in the script to print this character.

#!/bin/bash#Declare a string variablebook_name="Bash quick reference"#Declare a number variableprice=6#Print the concatenated stringecho "The price of $book_name is \$$price"

Concatenate string with special character

Using the script we have concatenated one string variable with the value, 'Bash quick reference' and the number variable with the value, 6 by adding a special character, '$'.

9. Concatenating string variables by printf

The following script will concatenate two string variables with a hyphen (-) in the script. Here, the `printf` command has been used to print the concatenated string. The formatting specifier, “%s” has been used in the printf command for denoting string value.

#!/bin/bash#Declare two string variablescourse_code="CSE407"course_name="Unix"#Print the concatenated stringprintf "%s-%s\n" $course_code $course_name

Concatenate string using printf

In the example script we have concatenated two string variables with the value, 'CSE407' and 'Unix' by adding hyphen(-).

Conclusion

String concatenation tasks have been shown here by using multiple bash scripts. You can follow any of the ways shown in this tutorial for concatenating string variables based on your programming requirements.

Last updated