Concatenation is the process of joining two values together.

Different programming languages deal with string concatenation in different ways.

Bash offers a couple of methods of concatenating two strings.

desktop setup with code on screen

Take a look at how you could join strings together in Bash.

Using the += Operator

you might add two strings or variables using the += operator in Bash.

Use echo to print out the resultant string then.

concatenating strings with compound operator

If you want to concatenate the values of two variables, you could adapt this method.

While this might not be the optimal approach, it still does the job.

Here’s how the code should look:

The output should be “Manchester City”.

concatenating literals with variables

you’re able to also concatenate string literals to variables by using parameter expansion.

Here’s how to do it:

The output should be “Erling Haaland plays in Manchester City”.

This is because Bash treats values as strings unless specified otherwise.

Here’s an example:

The output of this program should be “Hundred is 100”.

Now you know all the best approaches to concatenating strings in Bash.

String concatenation is one of the fundamental skills you should probably write Bash programs.

A solid understanding of the basics will help you master shell scripting.