Concatenate Strings in Bash

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
Short on time?Beta Get the gist of this 7-minute read in seconds, then ask follow-up questions

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:

bash
first='Hello'
second='World'
result="${first} ${second}"
echo "$result"
output
Hello World

Bash concatenates through adjacency — no operator sits between the expansions. To join without a space, omit it from the quotes:

bash
result="${first}${second}"
echo "$result"
output
HelloWorld

Store the result in another variable the same way:

bash
greeting="${first} ${second}"
echo "$greeting"
output
Hello World

A timestamped backup filename uses the same pattern — you supply the hyphen and extension:

bash
prefix='backup'
ts='20260909-1915'
file="${prefix}-${ts}.tar.gz"
echo "$file"
output
backup-20260909-1915.tar.gz

Double 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.

bash
first='Hello'
second='World'
result="$first $second"
echo "$result"
output
Hello World

Single quotes around the assignment prevent expansion:

bash
result='$first $second'
echo "$result"
output
$first $second

Use 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:

bash
name='backup'
file="${name}.tar.gz"
label="${name}_daily"
echo "$file"
echo "$label"
output
backup.tar.gz
backup_daily

Without braces, Bash may read the longest valid parameter name. $VAR1_suffix looks for VAR1_suffix, not VAR1 plus _suffix:

bash
VAR1=Hello
VAR2=World
echo "unbraced: [$VAR1_$VAR2]"
echo "braced:   [${VAR1}_${VAR2}]"
output
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:

bash
message='Hello'
message+=' World'
echo "$message"
output
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:

bash
host='db01'
port='5432'
endpoint="${host}:${port}"
echo "$endpoint"
output
db01:5432

For CSV-style fields, include the comma explicitly:

bash
first='Alice'
second='Admin'
result="${first},${second}"
echo "$result"
output
Alice,Admin

You can also build the same line incrementally with +=:

bash
result=''
result+='Alice'
result+=','
result+='Admin'
echo "$result"
output
Alice,Admin

Concatenate 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.

bash
status='ok'
printf -v msg 'host %s: %s' db01 "$status"
echo "$msg"
output
host db01: ok

To join values with a repeated separator, trim the trailing delimiter after assignment:

bash
printf -v joined '%s|' a b c
echo "${joined%|}"
output
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:

bash
file="backup-$(date +%F).tar.gz"
echo "$file"
output
backup-2026-09-09.tar.gz

The same pattern works in two steps:

bash
date=$(date +%F)
file="backup-${date}.tar.gz"
echo "$file"
output
backup-2026-09-09.tar.gz

One 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:

bash
part1=$(printf 'first\n')
part2=$(printf 'second\n')
combined="${part1}${part2}"
printf 'combined=[%s]\n' "$combined"
output
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:

bash
result=''

for word in Bash is easy; do
  result+="$word "
done

echo "${result% }"
output
Bash is easy

Each 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:

bash
printf 'demo line\n' > 'my report.txt'

Store the command and arguments in an array so the filename stays one word:

bash
file='my report.txt'
cmd=(cat -- "$file")
"${cmd[@]}"
output
demo line

Have 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.

Finished reading?Beta Get the key takeaways in seconds, then ask follow-up questions
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with 15+ years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)