Bash Concatenate Strings Tutorial

Bash provides very similar features to the programming languages. Concatenating strings are one of the most popular features of the bash. The string concatenation is simply joining multiple strings together or adding all strings into the first string. There are different methods for concatenating strings in bash and we will examine some of them.

Concatenate Using Variables

We can use variables to concatenate strings. All variables should be strings. These variables are used inside double quotes by using the $ sign. In the following example, we create 3 string variables and then concatenate them into the result variable.

name="Ahmet"
middlename="Ali"
surname="Baydan"

result = "$name $middlename $surname"

echo $result.
Ahmet Ali Baydan

Concatenate Using Literal Strings

Similar to the previous example we can use literal strings in order to concatenate strings. We can use the $ and { } curly brackets to express variables.

name="Ahmet"
middlename="Ali"

result = "${name} ${middlename} Baydan"

echo $result.

Concatenate Using += Operator

The += operator is very popular in different programming languages. The bash also provides the += operator to concatenate multiple strings.

name="Ahmet"
middlename="Ali"
surname="Baydan"

result+="${name}"

result+="${middlename}"

result+="${surname}"

echo $result.
Ahmet Ali Baydan
See also  "ip route add" - Add Route In Linux

Leave a Comment