How to Use Bash to Concatenate Strings

Many Bash scripts you’ll write will need to work with text, so make sure you understand this basic operation.

desktop setup with screen code
How to Use Bash to Concatenate Strings

Concatenation is the process of joining two values. String concatenation is an integral part of programming and you will find a use for it in all kinds of software.

Different programming languages ​​deal with string concatenation in different ways. Bash offers a couple of methods to concatenate two strings.

Take a look at how you can join strings in Bash.

Using the += operator

You can add two strings or variables using the += operator in Bash. First, declare a Bash variable that contains the first part of the string and, using the += operator, concatenate it with the second part of the string. Use echo to print the resulting string then. This is how you can concatenate strings in bash with the += operator:

#!/usr/bin/bash

s="Hello"
s+=" World, from MUO"
echo "$s"

The output should return “Hello World, from MUO”:

concatenate strings with compound operator
How to Use Bash to Concatenate Strings

In the example, you have concatenated a string variable with a string literal. If you want to concatenate the values ​​of two variables, you can adapt this method. Replace the string literal with the second variable you want to concatenate like this:

#!/usr/bin/bash

s="Merry"
d=" Christmas"
s+=$d
echo "$s"

Once you run your shell script, you should get the output “Merry Christmas”.

Concatenate strings by placing them sequentially

The easiest way to concatenate two or more strings or variables is to write them successively. While this might not be the optimal approach, it still works. This is how the code should look like:

#!/usr/bin/bash

s="Manchester"
b="City"
echo "$s $b"

The result should be “Manchester City”. You can also concatenate string literals to variables using parameter expansion. Here is how to do it:

#!/usr/bin/bash

s="Manchester City"
c="Erling Haaland plays in ${s}"
echo "$c"

The result should be “Erling Haaland plays for Manchester City”.

concatenate literals with variables
How to Use Bash to Concatenate Strings

Concatenate strings with numbers

In Bash, you can easily concatenate strings and numbers without running into data type mismatch errors. This is because Bash treats values ​​as strings unless otherwise specified. A variable with a value of “3” can be treated as an integer in a language like Python, but Bash will always treat it as a string value.

You can concatenate a string and a number using the += operator or by writing them sequentially. Here’s an example:

#!/usr/bin/bash

a="Hundred is "
a+=100
echo "$a"

The output of this program should be “One hundred is 100”. Now you know all the best approaches to concatenating strings in Bash.

Learn the basics of Bash Scripting

Bash scripts are useful for automating mundane and critical tasks. With Bash, you can write mini shell programs to help you maintain your system or server.

String concatenation is one of the fundamental skills you need to write Bash programs. A solid understanding of the basics will help you master shell scripting.

Leave a Reply

Your email address will not be published. Required fields are marked *