How to Concatenate String Variables in Bash [Join Strings]
Last updated
Last updated
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.
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.
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"
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.
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"
In the script we have concatenated one string variable with the value, 'Logitech Keyboard' and the number variable with the value, 1078.
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"
Here we have concatenated three string variables with the values, 'Tic-', 'Tac-', and 'Toe'.
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"
Using the script we have concatenated three string variables with the values, 'Linux', 'Windows', and 'Mac' by adding a separator, ':'.
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
In the script we have concatenated three string variables with the values, 'Bash', 'Python', and 'Perl' by using the shorthand operator, '+='.
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"
You can see we have concatenated two string variables with the values, '07856', and 'Nehal Sharif' by adding newline(\n).
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"
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, '$'.
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
In the example script we have concatenated two string variables with the value, 'CSE407' and 'Unix' by adding hyphen(-).
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.