1. Overview

Concatenating string variables is a common operation in shell script programming. Often, we want to append new information to a string that already exists.

In this quick tutorial, we’ll explore two methods for appending a string variable to itself in Bash.

2. The += Operator in Bash

Appending a string variable to itself in Bash is a special case of string concatenation.

We’ll start with a simple example where we need to build a full user name from name parts during the course of running a script.

For instance, we might first encounter the user’s first name and begin building the full name using that piece of information:

$ firstName='John'
$ fullName=$firstName
$ echo $fullName
John

In the above example, we first assign a value to the firstName variable. Then we assign $firstName to fullName. Finally, we use the echo command to verify that fullName has the value we expect. We use echo for this purpose throughout the following examples to check the value of fullName.

Later in the script, we might also have access to the user’s last name. In Bash, we can use the += operator to append a string to an existing string variable, i.e., the last name to the full name:

$ lastName='Doe'
$ fullName+=$lastName
$ echo $fullName
JohnDoe

Here, we first assign a value to lastName and then append $lastName to the existing fullName variable using the += operator.

In this case, we probably want a space between the first and last names, so we can use the += operator twice:

$ lastName='Doe'
$ fullName+=' '
$ fullName+=$lastName
$ echo $fullName
John Doe

In this example, we again start by assigning a value to lastName. Then, we append a space using the += operator and finish by appending $lastName to fullName, also using the += operator.

2.1. Limitations of the += Operator

As we can see from the examples above, the += operator is a very handy tool for building a string piece by piece. Yet, there are a couple of limitations with this operator:

  • while Bash supports the use of += for strings, the operator isn’t available in all Linux shells
  • the += operator doesn’t work for numbers in Bash the way we might expect

In the following Bash script, for example, we’re adding two numbers together:

$ num=100
$ num+=200
$ echo $num
100200

First, we assign num the value of 100. Then we add 200 to num using the += operator. We might expect num to be equal to 300 at this point. When we use echo $num to check its value, though, we see a different result.

Because Bash sees both operands as strings, the += operator doesn’t actually add numbers together. Rather, it concatenates the string values, just as in the user name examples from the previous section.

If we want to add the two numbers together in Bash, rather than concatenate them as strings, we need to use arithmetic expansion:

$ num=100
$ ((num+=200))
$ echo $num
300

Here, the double parentheses tell Bash to evaluate the enclosed text as an arithmetic expression rather than concatenating them as strings.

3. Append a String Variable to Itself in Any Linux Shell

While the += operator is convenient for concatenating strings in Bash shells, it won’t work for all shells. We need to take a different approach for a generalized solution.

Coming back to our simple example, we begin by assigning our fullName variable the string value for firstName:

$ firstName='John'
$ fullName=$firstName
$ echo $fullName
John

In general, we can concatenate two strings in a Linux shell script by placing them next to each other in an assignment statement.

So, when we’re ready to append the last name to our existing fullName variable:

$ lastName='Doe'
$ fullName=$fullName$lastName
$ echo $fullName
JohnDoe

Here, we first assign a value to lastName. Then, we assign fullName the existing value of fullName and append the value of lastName by simply appending one variable to the other.

As in the cases above where we used the += operator, we might want to add a space between the name pieces:

$ lastName='Doe'
$ fullName=$fullName' '$lastName
$ echo $fullName
John Doe

In this example, we again first assign a value to lastName. After that, we assign fullName the existing value of $fullName and append both a space and the value of $lastName.

Maybe we might want to add a middle name:

$ lastName='Doe'
$ fullName=$fullName' Lee '$lastName
$ echo $fullName
John Lee Doe

In this case, we append a middle name with spaces around it to fullName before appending $lastName.

We could also assign the middle name to a variable and use that to construct the full name:

$ lastName='Doe'
$ middleName='Lee'
$ fullName=$fullName' '$middleName' '$lastName
$ echo $fullName
John Lee Doe

In this case, we first assign values to lastName and middleName. Then, we append $middleName and $lastName to fullName, with spaces between the name parts.

To avoid confusion with single quotes seemingly surrounding our variables and simplify our assignment statement, we can use double quotes around the entire right-hand side:

$ lastName='Doe'
$ middleName='Lee'
$ fullName="$fullName $middleName $lastName"
$ echo $fullName
John Lee Doe

The double quotes in this example allow for variable interpolation while treating non-variable text as string literals.

The assignment statement for fullName substitutes variable values for their names, yielding the expected result.

4. Conclusion

In this article, we examined two methods for appending a string variable to itself in Bash.

First, we saw that the += operator is a quick and convenient option, but understood that it might not work for all shells. Further, it can lead to confusing results when a shell needs to perform both arithmetic and string manipulation.

Next, we explored the more general concatenation method of placing string variables side by side, which works for all Linux shells.