Bash Scripting
  • scripting list
  • How to Make Bash Script Executable Using Chmod
  • Shell Script to Check Linux Server Health
  • How to Concatenate String Variables in Bash [Join Strings]
  • How to Do Bash Variable Substitution (Parameter Substitution)
  • Bash Parameter Expansion with Cheat Sheet
  • Bash getopts with Examples
  • How to Pass all Arguments in Bash Scripting
  • Bash Function Return Value
  • Bash Loop Through Lines in a File
  • Bash readarray with Examples
  • Bash let with Examples
  • Bash expr with Examples
  • Bash read password
  • Bash for Loop Range Variable
  • Bash Arrays Explained
  • Ways to Find Bash Array Length
  • Bash Split String by Delimiter
  • Bash if Options
  • Bash If Statements for String Comparison
  • Debugging Techniques for Bash Scripts
  • Determining if a Bash String is Empty
  • Bash if Statement with Multiple Conditions
  • Meaning of Shebang in Bash Scripts
  • How to Comment Code in Bash Script
  • How to Read CSV File in Bash
  • Bash Scripting: How to Check if File Exists
  • Bash If Else Statements: Examples and Syntax
  • Bash Scripting: How to Check if Directory Exists
  • Bash eval Command with Examples
  • How to Use Sleep Command in Bash Scripting
  • Bash Associative Arrays with Examples
  • Bash Script for Counting Lines in a File
  • How to Use While Loop in Bash for Efficient Scripting
  • Bash basename Command with Examples
  • How to Create Multiline Strings in Bash
  • How to Use Bash if With && Operator
  • 50 Bash Script Examples to Kickstart Your Learning
  • Case statement in Bash Shell Scripting
  • Trimming White Space in Bash
  • How to Extract Filename from Absolute Path in Bash
  • How to Get Directory Path in Bash
  • Extract Extension from File Path in Bash
  • Extract Filename without Extension from Full Path in Bash
  • Bash for Each File in a Directory
  • Bash for Loop with Array
  • Bash Continue – Using with for Loop
  • Bash Backticks vs Dollar Parentheses $()
  • How to Assign Variable in Bash
  • How to Assign Variable in Bash
  • Bash Division Explained
  • Bash Modulo (Division Remainder)
  • Bash While Read Line by Line
  • Bash shift Command
  • Bash Looping Through Array of Strings
  • Bash read Command with Examples
  • Bash Check Empty Array
  • Using Bash For Loops to Iterate Over a List of Strings
  • Bash Break – Using with For Loop
  • How to Use seq With for Loop in Bash
  • How to Use $@ in Bash Scripting
  • Get the Current Script Directory in Bash
Powered by GitBook
On this page
  • What is String Concatenation?
  • Conclusion

How to Concatenate String Variables in Bash [Join Strings]

PreviousShell Script to Check Linux Server HealthNextHow to Do Bash Variable Substitution (Parameter Substitution)

Last updated 1 year ago

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"

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"

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"

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"

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

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"

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"

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

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.

concatenate string literals
Concatenate string with newline
concatenate string with number
Concatenate string without any separator
Concatenate string using operator (+=)
Concatenate string with separator
Concatenate string with special character
Concatenate string using printf