[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMay 29, 2023

Bash Concatenate Strings

Bash Concatenate Strings

In this tutorial, we will explain how to concatenate strings in Bash.

One of the most commonly used string operations is concatenation. String concatenation is just a fancy programming word for joining strings together by appending one string to the end of another string.

 

Concatenating Strings

The simplest way to concatenate two or more string variables is to write them one after another:

VAR1="Hello,"
VAR2=" World"
VAR3="$VAR1$VAR2"
echo "$VAR3"

The last line will echo the concatenated string:

Hello, World

You can also concatenate one or more variable with literal strings:

VAR1="Hello, "
VAR2="${VAR1}World"
echo "$VAR2"
Hello, World

In the example above variable VAR1 is enclosed in curly braces to protect the variable name from surrounding characters. When the variable is followed by another valid variable-name character you must enclose it in curly braces ${VAR1}.

To avoid any word splitting or globbing issues you should always try to use double quotes around the variable name. If you want to suppress variable interpolation and special treatment of the backslash character instead of double use single quotes.

Bash does not segregate variables by “type”, variables are treated as integer or string depending on contexts. You can also concatenate variables that contain only digits.

VAR1="Hello, "
VAR2=2
VAR3=" Worlds"
VAR4="$VAR1$VAR2$VAR3"
echo "$VAR4"
Hello, 2 Worlds

 

Concatenating Strings with the += Operator

Another way of concatenating strings in bash is by appending variables or literal strings to a variable using the += operator:

VAR1="Hello, "
VAR1+=" World"
echo "$VAR1"
Hello, World

The following example is using the += operator to concatenate strings in bash for loop :

languages.sh
VAR=""
for ELEMENT in 'Hydrogen' 'Helium' 'Lithium' 'Beryllium'; do
  VAR+="${ELEMENT} "
done

echo "$VAR"
Hydrogen Helium Lithium Beryllium

 

Conclusion

Concatenating string variables is one of the most fundamental operations in Bash scripting. After reading this tutorial, you should have a good understanding of how to concatenate strings in Bash. You can also check our guide about comparing strings .

Keep reading
How to Compare Strings in Bash
March 12, 2023

How to Compare Strings in Bash

How To Compare Strings In Bash This tutorial describes how to compare strings in Bash. When writing Bash scripts you will often need to compare two strings to check if they are equal or not. Two strings are equal when they have the same length and contain the same sequence of characters.…

Read article
How to use Whoami Command
March 12, 2023

How to use Whoami Command

How To Use Whoami Command In this article, we will cover the whoami command. As its name suggests, the whoami command prints the user name of the effective user ID. In other words, it displays the name of the currently logged-in user. How to Use the whoami Command The syntax for the whoami command…

Read article
How to Mount Windows Share on Linux using CIFS
March 10, 2023

How to Mount Windows Share on Linux using CIFS

How To Mount Windows Share On Linux Using CIFS In this tutorial, we will explain how to manually and automatically mount Windows shares on Linux systems. The Common Internet File System (CIFS) is a network file-sharing protocol. CIFS is a form of SMB. On Linux and UNIX operating systems, a Windows…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00