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 variable
strvar1="Bash"
#Declare another string variable
strvar2="Script"
#Print the concatenated string
echo "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.
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 words
item_name="Logitech Keyboard"
#Declare a number variable
item_code=1078
#Print the concatenated string
echo "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.
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 variables
strvar1="Tic-"
strvar2="Tac-"
strvar3="Toe"
#Print the concatenated string
echo "$strvar1$strvar2$strvar3"
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 variables
strvar1="Linux"
strvar2="Windows"
strvar3="Mac"
#Declare a string with a separator value
separator=":"
#Print the concatenated string
echo "$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, ':'.
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 variable
output="Selected languages are:"
#Print a prompt message
echo "Type three programming languages:"
#Take three input values
read lang1 lang2 lang3
#Concatenate the string values
output+="$lang1, $lang2,and $lang3"
#Print the concatenated string
echo -e $output
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 variables
id="07856"
applicant_name="Nehal Sharif"
echo "The concatenated values are:"
#Print the concatenated string
echo -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).
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 variable
book_name="Bash quick reference"
#Declare a number variable
price=6
#Print the concatenated string
echo "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, '$'.
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 variables
course_code="CSE407"
course_name="Unix"
#Print the concatenated string
printf "%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(-).
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.
Comments