| Tested on | RHEL 10.2 (Coughlan) |
|---|---|
| Package | bash 5.2.26-6.el10 |
| Applies to | Linux distributions with Bash installed |
| Privilege | Normal user |
| Scope | Bash string concatenation with adjacent expansion, ${var} boundaries, +=, separators, printf -v, quoting, command-substitution caveats, and loop accumulation. |
| Related guides | Bash shell script tutorial Shell scripting course hub |
Bash has no dedicated + operator for strings. You join text by placing variables or quoted literals next to each other, appending with +=, or formatting with printf.
Bash never inserts a separator between pieces — a space, comma, or colon only appears when you put it in the string. The examples below cover variable joins, literal suffixes, delimiters, command output, and the quoting and brace mistakes that show up most often in real scripts.
Concatenate Two Strings or Variables in Bash
Place the values next to each other in the same assignment or echo line:
first='Hello'
second='World'
result="${first} ${second}"
echo "$result"Hello WorldBash concatenates through adjacency — no operator sits between the expansions. To join without a space, omit it from the quotes:
result="${first}${second}"
echo "$result"HelloWorldStore the result in another variable the same way:
greeting="${first} ${second}"
echo "$greeting"Hello WorldA timestamped backup filename uses the same pattern — you supply the hyphen and extension:
prefix='backup'
ts='20260909-1915'
file="${prefix}-${ts}.tar.gz"
echo "$file"backup-20260909-1915.tar.gzDouble quotes vs single quotes
Double quotes expand variables; single quotes preserve $variable literally — single vs double quotes in Bash walks through when each form is safer.
first='Hello'
second='World'
result="$first $second"
echo "$result"Hello WorldSingle quotes around the assignment prevent expansion:
result='$first $second'
echo "$result"$first $secondUse double quotes when you want the variable values in the result. For more on when each quote type applies, see Bash variables.
Concatenate a Variable with Literal Text
To append literal text after a variable, write ${var} before the suffix:
name='backup'
file="${name}.tar.gz"
label="${name}_daily"
echo "$file"
echo "$label"backup.tar.gz
backup_dailyWithout braces, Bash may read the longest valid parameter name. $VAR1_suffix looks for VAR1_suffix, not VAR1 plus _suffix:
VAR1=Hello
VAR2=World
echo "unbraced: [$VAR1_$VAR2]"
echo "braced: [${VAR1}_${VAR2}]"unbraced: [World]
braced: [Hello_World]Use ${name} when a letter, digit, or underscore immediately follows the variable name, because Bash may treat those characters as part of the variable name. For example, ${name}_backup clearly means the value of name followed by _backup. Braces are optional before characters such as -, ., /, or : because those characters cannot be part of a normal Bash variable name — "$name-file" and "$name.txt" are unambiguous.
Append a String with +=
+= appends text to the current value of a variable:
message='Hello'
message+=' World'
echo "$message"Hello World+= appends text to a string variable. With an array, var+=(item) instead adds a new array element.
Concatenate Strings with a Separator
Bash does not add separators for you. Put the delimiter in the quoted string:
host='db01'
port='5432'
endpoint="${host}:${port}"
echo "$endpoint"db01:5432For CSV-style fields, include the comma explicitly:
first='Alice'
second='Admin'
result="${first},${second}"
echo "$result"Alice,AdminYou can also build the same line incrementally with +=:
result=''
result+='Alice'
result+=','
result+='Admin'
echo "$result"Alice,AdminConcatenate Strings with printf -v
Use printf -v when you need to build formatted text and assign the result directly to a variable instead of printing it. See Bash echo vs printf for format-string details.
status='ok'
printf -v msg 'host %s: %s' db01 "$status"
echo "$msg"host db01: okTo join values with a repeated separator, trim the trailing delimiter after assignment:
printf -v joined '%s|' a b c
echo "${joined%|}"a|b|c${joined%|} removes the trailing | that the repeated %s| format adds after the last field. GNU Bash assigns the formatted result directly to the named variable with printf -v.
Concatenate Command Output with a String
Command substitution $(...) embeds command output inside a string:
file="backup-$(date +%F).tar.gz"
echo "$file"backup-2026-09-09.tar.gzThe same pattern works in two steps:
date=$(date +%F)
file="backup-${date}.tar.gz"
echo "$file"backup-2026-09-09.tar.gzOne caveat: command substitution removes trailing newline characters from the captured output. If you concatenate two command results without an explicit separator, the lines can run together:
part1=$(printf 'first\n')
part2=$(printf 'second\n')
combined="${part1}${part2}"
printf 'combined=[%s]\n' "$combined"combined=[firstsecond]Add the separator you need in the string — for example $'\n' between parts, or printf -v when you want a formatted multiline result.
Concatenate Strings in a Loop
You can use += inside a loop when each iteration needs to add text to the same variable:
result=''
for word in Bash is easy; do
result+="$word "
done
echo "${result% }"Bash is easyEach iteration appends the current word and a space. ${result% } removes the final space. The same pattern works in a while loop when you accumulate text across many iterations.
Common Bash String Concatenation Errors
| Problem | Cause | Fix |
|---|---|---|
$VAR_suffix expands incorrectly |
_ became part of the variable name |
${VAR}_suffix |
'$first$second' prints variables literally |
Single quotes disable expansion | "$first$second" with double quotes |
firstsecond instead of first second |
No separator was provided | "${first} ${second}" |
| Trailing comma or pipe on the joined string | Delimiter appended after every piece | ${value%,} or ${value%|} as appropriate |
firstsecond from two command outputs |
$(...) strips trailing newlines |
Add $'\n' or another separator explicitly |
cat/grep treats one path as multiple arguments |
Filename expanded without quotes | Use "$file"; use an array for dynamically built argument lists |
Do not use string concatenation to build a full command line. If a command needs dynamically generated arguments, store them in a Bash array and run "${cmd[@]}" so spaces and argument boundaries stay intact.
Create a file with a space in the name, then pass it to cat as one argument:
printf 'demo line\n' > 'my report.txt'Store the command and arguments in an array so the filename stays one word:
file='my report.txt'
cmd=(cat -- "$file")
"${cmd[@]}"demo lineHave an array instead of separate string variables? Split a string into an array covers the reverse path; joining many elements is a different operation from concatenating two strings. Use an array-aware joining method rather than manually appending every element.
Summary
Bash concatenates strings by placing expansions next to each other — there is no + operator. Use "${first} ${second}" for a space between values, ${var}suffix when literal text follows a name, and += when a loop accumulates text. Separators such as colons, commas, and newlines must appear in your quotes or format string.
Use double quotes when you need variable expansion; single quotes preserve $name literally. printf -v fits formatted labels and fixed placeholders. When you embed $(...), remember that trailing newlines are stripped from the captured output. For paths, messages, and CSV rows, string concatenation is appropriate; for command arguments with arbitrary paths, use quoted parameters or arrays instead of one rebuilt command string.
References
Frequently Asked Questions
How do I concatenate two variables in Bash?
Place them next to each other inside double quotes: result="${first}${second}" for no gap, or result="${first} ${second}" when you need a space between the values.
How do I concatenate a string and a variable?
Put the literal text after a braced expansion: file="${name}.tar.gz" or label="${name}_daily". Use ${name} when the next character is _, a letter, or a digit so Bash does not treat it as part of the variable name.
How do I append text to an existing Bash variable?
Use +=: message='Hello' then message+=' World'. Each += appends to the current value.
How do I concatenate strings with a space or delimiter?
Include the separator in the quoted string — "${first} ${second}" for a space, "${host}:${port}" for a colon, "${a},${b}" for a comma. Bash does not insert delimiters automatically.
Does Bash have a + operator for strings?
No. Join with adjacent expansions, append with +=, or format with printf or printf -v.
How do I add a newline between two Bash strings?
Insert $'\n' between the parts: "${part1}"$'\n'"${part2}". Command substitution alone does not preserve trailing newlines from captured output, so add the newline explicitly when you need it.

