← Back to Bash Course | Chapter 7: String Manipulation | Lesson 9 of 10

String Concatenation

Joining strings together in Bash is as simple as writing them next to each other or appending with +=, with no special concatenation operator needed.

Basic Adjacent Concatenation

Bash concatenates strings simply by placing them next to each other with no operator in between, whether that's two variables, a variable and literal text, or several pieces combined. Wrapping the whole thing in double quotes keeps it as one clean result.

Example: Basic Adjacent Concatenation

bash
#!/bin/bash
first="Hello"
second="World"
combined="$first, $second!"
echo "$combined"

Appending with +=

The += operator appends new text onto the end of an existing string variable, rather than replacing its current value the way plain = would. This is convenient for building up a string incrementally, such as inside a loop.

Example: Appending with +=

bash
#!/bin/bash
message="Processing"
message+=": "
message+="step 1"
message+=", step 2"
echo "$message"

Building a String Inside a Loop

Repeatedly using += inside a loop is a common way to accumulate a result string from multiple pieces, such as joining array elements with a separator. This avoids the overhead and complexity of an external tool just to join a list of values.

Example: Building a String Inside a Loop

bash
#!/bin/bash
items=(apple banana cherry)
joined=""
for item in "${items[@]}"; do
    if [ -z "$joined" ]; then
        joined="$item"
    else
        joined+=", $item"
    fi
done
echo "Joined list: $joined"

Concatenating Numbers as Strings

Since every Bash variable is text by default, concatenating things that look like numbers just glues their characters together rather than adding them, unlike using them inside $(( )) where they would be summed instead. Knowing which context you're in -- string concatenation vs. arithmetic -- avoids a common source of confusion.

Example: Concatenating Numbers as Strings

bash
#!/bin/bash
a="12"
b="34"
concatenated="$a$b"
sum=$((a + b))
echo "Concatenated (string): $concatenated"
echo "Sum (arithmetic): $sum"
Common Mistakes
  1. Looking for a + or . concatenation operator like in other languages; Bash concatenates simply by placing values adjacent to each other inside a string, with no operator at all.
  2. Forgetting that += appends to a string variable's existing value rather than replacing it, which is the opposite of what a plain = would do.
  3. Concatenating unquoted variables that might contain spaces or globs, which can trigger word splitting or pathname expansion instead of a clean literal join.
Chapter Summary
  • Concatenation happens automatically by placing values next to each other: combined="$a$b" or combined="$a $b".
  • var+="more text" appends to the end of an existing string variable's value.
  • Concatenating inside double quotes is the safest approach, since it prevents unwanted word splitting or globbing on the pieces being joined.
  • Numbers and strings concatenate the same way as any other values, since everything is text unless used inside an explicit arithmetic context.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.