Bash: Concatenate Strings Easily with Our Simple Guide

bash concatenate strings
bash concatenate strings

As we have iterated countless times in our previous Bash guides, Bash is a very powerful shell used in a lot of Unix based systems like Linux and macOS. One of the common Bash scripting tasks is string manipulation, including and particularly concatenating strings in Bash. In Bash, concatenate strings to make your text processing easy.

Concatenating (in simpler words: combining) strings is a basic task that can help you dynamically process user input, generate messages, or create filenames. In this guide, let us see the various methods in Bash to concatenate strings.

What is String Concatenation in Bash?

The term string concatenation refers to the process of combining two or more strings “end-to-end” into a single string. In programming and scripting, concatenation is commonly used to create dynamic strings, process text, and generate output.

If you have two strings “Unix” and “Men”, concatenating these two strings would get you “UnixMen”.

Bash String Concatenation Basics

Bash is one of the very straightforward programming languages when it comes to string concatenation. While other programming languages use operators like the “+” symbol, Bash allows concatenation by simply placing strings together. Let us give you one basic example:

str1="Unix"<br />str2="Men"<br />result="$str1$str2"<br />echo $result

Running this script will fetch the output: UnixMen

To explain a little bit, the strings “str1” and “str2” are concatenated without any symbols just by placing them next to each other. The result is stored in the variable “result”.

How Add a Space Between Strings in Bash

There can be cases where you would like to add a separator between the strings, like a space, underscore, or a hyphen. Do this by manually adding the required separator between the strings. To add an empty space between two strings, here is an example:

str1="Unix"<br />str2="Men"<br />result="$str1 $str2"<br />echo $result

Running this script will show the output as: Unix Men. Did you notice the space between the two strings without any operator or symbols? Bash is easy.

How to Add Variables in Bash String Concatenation

Along with spacebars, you can concatenate strings in Bash with variables. This type of Bash string concatenation will help you when you have to build strings dynamically based on different inputs and other script variables. Here is another simple example to get a greeting message:

greeting="Hello"<br />name="Unixmen"<br />message="$greeting, $name!"<br />echo $message

This script will fetch the output: Hello Unixmen! It is important to note that Bash has added both the spacebar and the exclamation mark to the output.

With the help of variables, you can create more dynamic and flexible scripts that can adapt to different inputs.

How to Concatenate Strings in a Loop

In advanced cases, you would require concatenating strings within a loop. An example of this scenario would be when you have to build a comma-separated list combining multiple strings or input values. Here is an example for the same:

items=(“Fedora” “Ubuntu” “CentOS”)
result=””
for item in “${items[@]}”; do
result=”$result$item,”
done
result=”${result%,}”
echo $result

The output will be Fedora,Ubuntu,CentOS.

Looking at the output, a loop is used to combine (concatenate) the items in the “items” array into a single string and then separated by comma.

How to Concatenate Strings in Bash with the += Operator

One more method to concatenate strings in Bash is by using the += operator. This operator appends the former string to the latter.

result="Unix"<br />result+=" Men"<br />echo $result

The result of this command will be: Unix Men. The difference between this method and the fairly simple first command in this article is just one use case. Prefer this method when you have to build a string incrementally. By incrementally, it means appending additional text or variables.

Concatenating Strings with Command Outputs

To include output of commands using command substitution, there is a way to in Bash. Command substitution allows you to execute a command and add its output as the string or a part of the string. Let us now see a simple example:

date=$(date +%Y-%m-%d)<br />result="Today's date is $date"<br />echo $result

The output will be (when this article is being proof-read):

Today's date is 2024-08-10

Wrapping Up

Concatenating strings in Bash is a very easy-to-master skill. In Bash, concatenate strings to work with static text, variables, and even command outputs. It becomes much easier to create dynamic and responsive Bash script that can handle a complex sect of tasks by mastering Bash concatenate strings.

Related Link

A community thread that discusses different bash concatenation strategies

Some More Articles that would be of Interest to You