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.

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

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

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

Using the += Operator

You can add two strings or variables using the += operator in Bash. First, declare a Bash variable containing the first part of the string, and using the += operator, concatenate it with the second part of the string. Use echo to print out the resultant string then. Here’s how you can concatenate strings in bash with the += operator:


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

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

concatenating strings with compound operator

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 literal string with the second variable you want to concatenate like this:


d=" Christmas"
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 down successively. While this might not be the optimal approach, it still does the job. Here’s how the code should look:


echo "$s $b"

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


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

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

concatenating literals with variables

Concatenate Strings With Numbers

In Bash, you can easily concatenate strings and numbers together without running into data type mismatch errors. This is because Bash treats values as strings unless specified otherwise. A variable with a value of “3” may 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:


a="Hundred is "
echo "$a"

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

Learn the Fundamentals of Bash Scripting

Bash scripts come in handy for automating both critical and mundane 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.


Source link

Leave a Reply

%d bloggers like this: