String Concatenation
In this page:
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
#!/bin/bash
first="Hello"
second="World"
combined="$first, $second!"
echo "$combined"
Login to try C/C++/Java/PHP code in the editor
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 +=
#!/bin/bash
message="Processing"
message+=": "
message+="step 1"
message+=", step 2"
echo "$message"
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
a="12"
b="34"
concatenated="$a$b"
sum=$((a + b))
echo "Concatenated (string): $concatenated"
echo "Sum (arithmetic): $sum"
Login to try C/C++/Java/PHP code in the editor
- 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. - Forgetting that
+=appends to a string variable's existing value rather than replacing it, which is the opposite of what a plain=would do. - Concatenating unquoted variables that might contain spaces or globs, which can trigger word splitting or pathname expansion instead of a clean literal join.
- Concatenation happens automatically by placing values next to each other:
combined="$a$b"orcombined="$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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: