HOWTO · Linux
How to Concatenate Strings in Bash
Discover how to concatenate strings in Bash easily with our comprehensive guide. Learn various methods, including basic concatenation, using the += operator, and incorporating command substitution. Master these techniques to enhance your Bash scripting skills and create dynamic outputs effortlessly. Perfect for beginners and experienced users alike, this article provides clear examples and explanations to help you navigate string manipulation in Bash.
On this page
In Bash, concatenate strings by placing quoted values next to each other, or append to an existing variable with +=. A space is included only when you put one in the value you assign.
Method 1: Basic Concatenation
For two values, place their quoted expansions together. Include any separator literally inside the quotes:
string1="Hello"
string2="World"
result="$string1 $string2"
printf '%s\n' "$result"
Here, result contains Hello World because the assignment includes a space between $string1 and $string2. Use quotes around expansions when passing a value to a command, as in printf '%s\n' "$result"; this keeps spaces and wildcard characters in the value instead of allowing word splitting or pathname expansion. Braces delimit a variable name when adjacent text could be read as part of that name: "${string1}_suffix". Bash variables do not have fixed string or integer types, so a numeric-looking value can also be included as text, for example number=42 followed by label="ID: $number".
Method 2: Using the += Operator
Use += to append the right-hand text to the current value of a variable:
greeting="Hello"
greeting+=" World"
printf '%s\n' "$greeting"
The leading space in " World" becomes part of the appended text, so greeting holds Hello World. The += operator updates the same variable; it is string appending here, not arithmetic addition.
Method 3: Concatenating Multiple Strings
Adjacent quoted expansions and literals can form one value. Put separators where they belong:
first="Bash"
second="is"
third="great!"
combined="$first $second $third"
printf '%s\n' "$combined"
The three expansions are joined in order, with the two literal spaces in the assignment providing the separators. If the desired format has no spaces, leave them out; Bash does not insert separators automatically.
Method 4: String Concatenation in Loops
Append each item in turn. This example places a space between items and therefore avoids a trailing space:
result=""
separator=""
for word in "Learning" "Bash" "is" "fun"
do
result+="${separator}${word}"
separator=" "
done
printf '%s\n' "$result"
The first item is appended without a separator. After each item, separator becomes one space, so later items are separated while the final value ends with fun. Replace the quoted words with your own loop values; quote expansions when inserting values that may contain spaces or shell wildcard characters.
Method 5: Using Command Substitution
Command substitution, written as $(...), runs a command and substitutes its standard output into the surrounding assignment:
name="John"
greeting="Hello, $(printf '%s' "$name")!"
printf '%s\n' "$greeting"
The command’s output is inserted between Hello, and !. Command substitution removes trailing newline characters from that output; quote values inside the command and quote the resulting variable when using it as an argument.
Conclusion
To concatenate in Bash, put quoted values side by side, add separators explicitly, or use += to append to an existing value. A loop can repeat the append operation, and $(...) can insert command output. Keep expansions quoted when passing values to commands so spaces and wildcard characters remain part of the value.
FAQ
-
How do I concatenate two strings in Bash?
Assign adjacent quoted expansions, such ascombined="${first}${second}", or append one withcombined+="$second". Include a space in the quoted text if the result needs one. -
Can I concatenate strings in a loop?
Yes. Append each item to a result variable with+=; add a separator deliberately between items if the joined value needs one. -
What is command substitution in Bash?
$(...)runs a command and inserts its standard output into the surrounding text. Trailing newline characters from that output are removed. -
How can I find a string’s length?
Use${#value}to get the length of a Bash variable’s value. Character counting can depend on the active locale; in theClocale, it counts bytes. -
Can I concatenate strings with special characters?
Yes. Double quotes preserve spaces and prevent wildcard expansion for variable values, while shell syntax such as$, backticks, and quotes may still need quoting or escaping when entered literally. For arbitrary text, choose quoting that prevents the shell from interpreting its characters before assignment.