How to Concatenate Strings in Bash

August 3, 2023

Introduction

In programming, concatenation is the process of combining two or more strings into a single one. Concatenation is widely used throughout various programming languages to build longer strings by appending one string to the end of another.

This process is helpful when you need to generate dynamic strings, format text with variables or placeholders, and generate reports, log, and display data.

In this tutorial, you will learn to concatenate strings in Bash.

How to concat strings in Bash - a tutorial.

Prerequisites

  • A system running Linux.
  • Access to the terminal (Ctrl+Alt+T).
  • A text editor for creating scripts.

Concatenating Strings Using Literal Strings

Concatenating strings using literal strings means combining fixed text with other variable strings. In most programming languages, such as Python or Java, you can perform concatenation using the + operator. In Bash, strings are concatenated using the = operator to assign the concatenated value to a new variable.

The following sections show how to concatenate multiple variables and numbers and strings.

Note: Read our tutorial on the cat command, which is short for concatenate, and often used to concatenate and display the contents of files.

Concatenation of Multiple Variables

Concatenating multiple variables involves combining more than two strings. Follow the steps below to create a Bash script that concatenates multiple variables together:

1. Create a new Bash script. In this tutorial, we will use the nano text editor:

nano multiple-variables.sh

2. Paste the following lines in the script:

#!/bin/bash
part1="Hello"
part2=" and"
part3=" welcome"
part4=" to"
part5=" phoenixNAP"
message="$part1$part2$part3$part4$part5"
echo "$message"

3. Save the file and exit the editor.

4. Make the script executable by changing the file permissions:

chmod +x multiple-variables.sh

5. Run the script:

./multiple-variables.sh
Example of concatenating multiple variables into one in Bash.

Bash concatenates all the variables into a single string and outputs the results.

Concatenation of Numbers and Strings

Concatenating numbers and strings involves converting numbers to strings before concatenation. Convert numbers to strings using the $(( )) syntax.

Follow the steps below:

1. Create a new Bash script and paste the following lines:

#!/bin/bash
age=30
message="I am "$age" years old."
echo "$message"

2. Save the file and make the script executable.

3. Run the script:

Concatenating numbers and strings in Bash.

The script converts the number from the variable to a string and concatenates it with the rest of the message.

Note: Always use double quotes around a variable name to avoid word splitting or globbing issues. Use single quotes to suppress variable interpolation and special treatment of the backslash character. Read our tutorial to learn more about the difference between using single and double quotes.

Concatenating Strings Using += Operator

The += operator also allows you to concatenate strings in Bash. The operator appends the right-hand string to the left-hand variable. The sections below show how to concatenate numeric strings and use the for loop to concatenate strings in Bash.

The sections below show how to concatenate numeric strings and concatenate strings using the Bash for loop.

Concatenation of Numeric Strings

Bash treats all variables as strings by default. Therefore, numeric strings can be concatenated directly without any special operations.

Follow the steps below:

1. Create a new Bash script and enter the following lines:

#!/bin/bash
num1="10"
num2="5"
num1+="5"  # Concatenate "5" to the num1 variable
result="$num1$num2"
echo "$result"

2. Save the file and change the permissions to make it executable.

3. Run the script:

Example of concatenating numeric strings.

In this example, the num1 variable initially holds the string "10". By using num1+="5", we append the string "5" to the existing value of num1. After that, the result variable holds the concatenated string "1055", which is the combination of num1 and num2.

Concatenation Using Bash for Loop

You can also use a for loop to concatenate strings in Bash. It is especially useful when joining elements from an array or iterating them over a sequence.

Follow the steps below to test:

1. Create a new script and paste the lines below:

#!/bin/bash
fruits=("apple" "banana" "orange" "grape")
message="I like: "
for fruit in "${fruits[@]}"; do
    message+="$fruit, "
done
message="${message%, }."  # Remove the trailing comma and space
echo "$message"

2. Save the script and make it executable.

3. Run the script:

Using the for loop to concatenate strings in Bash.

The for loop embedded in the Bash script concatenates the specified strings and outputs the result.

Conclusion

Concatenation is a powerful tool that helps users build dynamic messages, format output, and manipulate strings effectively. Bash offers various ways to concatenate strings, allowing you to manipulate and format them based on your needs.

For more tutorials on Bash, see how to use the Bash until loop or learn to use the Bash eval command to evaluate strings and execute them as shell commands.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Read Files Line by Line in Bash
March 17, 2022

This tutorial shows how to read a file line by line using the Bash shell. See five different methods and examples for processing a file's contents effectively.
Read more
How To Use The Bash read Command
February 21, 2022

The Bash read command reads a user's input or a file descriptor and allows splitting into different fields. Learn how to utilize the command in this guide.
Read more
How to Use the Bash let Command
January 20, 2022

The Bash let command is a built-in utility used for evaluating arithmetic expressions. Learn how to use the Bash let command in this tutorial.
Read more
Bash Math Operations Explained
April 14, 2022

Bash supports math and arithmetic operations, although with basic functionalities. This tutorial show how to maximize the use of math in Bash scripts.
Read more