Skip to main content
ubuntuask.com

Back to all posts

How to Concatenate Strings In Bash?

Published on
4 min read
How to Concatenate Strings In Bash? image

Best Bash Scripting Tools to Buy in October 2025

1 Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

BUY & SAVE
$23.74 $39.99
Save 41%
Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali
2 Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

  • AFFORDABLE PRICING FOR QUALITY READING WITHOUT BREAKING THE BANK.
  • ECO-FRIENDLY CHOICE BY PROMOTING BOOK REUSE AND SUSTAINABILITY.
  • HIDDEN GEMS: UNIQUE FINDS THAT ENRICH YOUR READING EXPERIENCE!
BUY & SAVE
$22.15 $44.99
Save 51%
Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))
3 Shell Scripting: Expert Recipes for Linux, Bash, and more

Shell Scripting: Expert Recipes for Linux, Bash, and more

BUY & SAVE
$38.49 $49.99
Save 23%
Shell Scripting: Expert Recipes for Linux, Bash, and more
4 Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting

Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting

BUY & SAVE
$37.98
Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting
5 Classic Shell Scripting

Classic Shell Scripting

BUY & SAVE
$25.73 $49.99
Save 49%
Classic Shell Scripting
6 Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming

Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming

BUY & SAVE
$2.99
Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming
7 BASH Guide

BASH Guide

BUY & SAVE
$0.99
BASH Guide
8 Mastering Shell for DevOps: Automate, streamline, and secure DevOps workflows with modern shell scripting

Mastering Shell for DevOps: Automate, streamline, and secure DevOps workflows with modern shell scripting

BUY & SAVE
$33.99
Mastering Shell for DevOps: Automate, streamline, and secure DevOps workflows with modern shell scripting
+
ONE MORE?

In Bash scripting, concatenating strings means combining two or more strings into a single string. This is usually achieved using the concatenation operator + or by simply placing the strings adjacent to each other.

There are several ways to concatenate strings in Bash:

  1. Using the + operator: string1="Hello" string2=" World" result=$string1$string2 echo $result Output: Hello World
  2. Using the += operator: string1="Hello" string2=" World" string1+=$string2 echo $string1 Output: Hello World
  3. Placing strings adjacent to each other: string1="Hello" string2=" World" result="$string1$string2" echo $result Output: Hello World

Note that if you want to concatenate strings inside a loop or within command substitution, you might need to use the printf command or the ${} syntax instead, depending on the situation.

How to concatenate strings with a delimiter in Bash?

To concatenate strings with a delimiter in Bash, you can use the printf command along with the %s format specifier. Here's an example:

#!/bin/bash

string1="Hello" string2="World" delimiter=","

result=$(printf "%s${delimiter}%s" "$string1" "$string2")

echo "$result"

In the above script, the printf command concatenates the two strings (string1 and string2) with the specified delimiter (delimiter). The resulting concatenated string is then stored in the result variable, and finally, it is printed using the echo command.

If you run the script, it will output:

Hello,World

Here, the delimiter "," is used to join the two strings, resulting in the concatenated string "Hello,World".

How to concatenate strings without using any built-in operators or functions in Bash?

In Bash, you can concatenate strings without using any built-in operators or functions by using variable expansion and redirection. Here's an example:

  1. Declare two variables with the desired strings:

string1="Hello" string2="World"

  1. Create a file descriptor for the variables:

exec 3<<<"$string1$string2"

  1. Read the file descriptor and store the concatenated string in a new variable:

concatenated="" while IFS= read -r -n 1 char; do concatenated+=$char done <&3

  1. Print the concatenated string:

echo "$concatenated"

This method uses the file descriptor 3 to redirect the concatenated string to the while loop, which reads each character and appends it to the concatenated variable. Finally, the concatenated string is printed using echo.

How to concatenate strings while ignoring leading or trailing whitespaces in Bash?

To concatenate strings while ignoring leading or trailing whitespaces in Bash, you can use the following approach:

  1. Create a variable to hold the concatenated string.

concatenated=""

  1. Iterate through the input strings using a loop.

for input_string in "$@"; do

Logic to ignore leading/trailing whitespaces

done

  1. Inside the loop, utilize parameter expansions to remove the leading/trailing whitespaces.

trimmed_string="${input_string#"${input_string%%[![:space:]]*}"}" trimmed_string="${trimmed_string%"${trimmed_string##*[![:space:]]}"}"

  1. Concatenate the trimmed string to the existing concatenated string.

concatenated+=" $trimmed_string"

  1. After the loop, remove any leading/trailing whitespaces from the final concatenated string.

concatenated="${concatenated#"${concatenated%%[![:space:]]*}"}" concatenated="${concatenated%"${concatenated##*[![:space:]]}"}"

  1. Print the final concatenated string.

echo "$concatenated"

Here's the complete code snippet:

concatenated=""

for input_string in "$@"; do trimmed_string="${input_string#"${input_string%%[![:space:]]*}"}" trimmed_string="${trimmed_string%"${trimmed_string##*[![:space:]]}"}" concatenated+=" $trimmed_string" done

concatenated="${concatenated#"${concatenated%%[![:space:]]*}"}" concatenated="${concatenated%"${concatenated##*[![:space:]]}"}"

echo "$concatenated"

You can then execute the script and provide input strings as command-line arguments:

$ bash concatenate_strings.sh " Hello" "World " " from " " Bash " Hello World from Bash

How to concatenate strings using the += operator in Bash?

To concatenate strings using the += operator in Bash, you can follow these steps:

  1. Declare a variable and initialize it with a string value. str="Hello"
  2. Use the += operator to concatenate another string to the existing value of the variable. str+=" World"
  3. Print the concatenated string. echo $str

This will output:

Hello World

By using the += operator, you can add a new string to the end of an existing string variable, modifying its value and creating a concatenated string.

How to concatenate strings with special characters in Bash?

To concatenate strings with special characters in Bash, you can use the following syntax:

string1="Hello" string2="World!" special_char='*'

concatenated_string="$string1$special_char$string2" echo $concatenated_string

In this example, the special_char variable is assigned a value of *. The two strings string1 and string2 are then concatenated using the $ symbol. The result is stored in the concatenated_string variable, and it is then printed using the echo command.

The output of the above code will be:

Hello*World!

You can customize the special_char to any special character you want.

How to concatenate strings with whitespace in Bash?

In Bash, you can concatenate strings with whitespace by enclosing the strings in double quotes and using the concatenation operator + or by using variable interpolation. Here are a few examples:

  1. Using the concatenation operator:

str1="Hello," str2="world!" result=$str1" "$str2 echo $result # Output: Hello, world!

  1. Using variable interpolation:

str1="Hello," str2="world!" result="$str1 $str2" echo $result # Output: Hello, world!

Both methods include the whitespace between quotes, ensuring a space between the concatenated strings. The output will be "Hello, world!".