How to Concatenate Strings in Bash

When writing Bash scripts, you will often need to build a string from several parts: joining a directory path with a file name, composing a log message, or assembling a command from variables. Joining two or more strings together by placing one after another is called concatenation.
This article covers the main ways to concatenate strings in Bash with practical examples.
Concatenating Strings with Juxtaposition
The simplest way to concatenate strings is to place variables next to one another:
VAR1="Hello,"
VAR2=" World"
VAR3="$VAR1$VAR2"
echo "$VAR3"Hello, WorldYou can also mix variables with literal strings:
VAR1="Hello,"
VAR2="${VAR1} World"
echo "$VAR2"Hello, WorldIn the example above, VAR1 is enclosed in curly braces. This is required when a variable is immediately followed by a character that could be part of a valid variable name. Without the braces, Bash tries to expand the entire token as a single variable:
VAR="Hello"
echo "$VARWorld" # expands $VARWorld, which is empty
echo "${VAR}World" # expands $VAR, then appends WorldThe first command prints an empty line because VARWorld is unset. The second command prints the concatenated value:
HelloWorldAlways quote variables in double quotes to prevent word splitting and glob expansion. Use single quotes when you want to suppress variable expansion entirely; characters inside single quotes are treated as literals.
By default, Bash variables can hold text or numbers. A value containing only digits can be concatenated like any other string:
VAR1="Hello, "
VAR2=2
VAR3=" Worlds"
VAR4="$VAR1$VAR2$VAR3"
echo "$VAR4"Hello, 2 WorldsAppending Strings with the += Operator
The += operator appends a string to the end of an existing variable:
VAR1="Hello,"
VAR1+=" World"
echo "$VAR1"Hello, WorldThe += operator appends text even when both values are numbers:
NUM=5
NUM+=10
echo "$NUM"510Bash joins 5 and 10 into the string 510. If you declare a variable with declare -i, Bash treats its value as an integer. The += operator then adds the numbers:
declare -i NUM=5
NUM+=10
echo "$NUM"15For a broader look at math in Bash, see our guide on Bash arithmetic .
The += operator is particularly useful in loops for building up a string incrementally. The following example uses it in a bash for loop
to join a list of words:
VAR=""
for ELEMENT in 'Hydrogen' 'Helium' 'Lithium' 'Beryllium'; do
VAR+="${ELEMENT} "
done
echo "$VAR"Hydrogen Helium Lithium BerylliumConcatenating Strings with printf
The printf command
with -v formats and assigns a string directly to a variable without spawning a subshell:
VAR1="Hello,"
VAR2=" World"
printf -v VAR3 '%s%s' "$VAR1" "$VAR2"
echo "$VAR3"Hello, WorldThis approach is especially useful when you need to include a separator or control the format precisely:
printf -v RESULT '%s, %s' "Hello" "World"
echo "$RESULT"Hello, WorldUnlike a command substitution such as VAR=$(printf ...), printf -v does not fork a subshell, making it slightly more efficient in tight loops.
Quick Reference
For a printable quick reference, see the Bash cheatsheet .
| Method | Example |
|---|---|
| Juxtaposition | VAR3="$VAR1$VAR2" |
| Curly brace delimiters | VAR3="${VAR1}suffix" |
+= operator | VAR1+=" World" |
printf -v | printf -v VAR3 '%s%s' "$VAR1" "$VAR2" |
| With separator | printf -v VAR3 '%s, %s' "$VAR1" "$VAR2" |
Troubleshooting
Variable value does not appear in the concatenated string
You are likely using single quotes. Single quotes suppress all variable expansion. Change '$VAR1$VAR2' to "$VAR1$VAR2" to allow expansion.
Concatenated string breaks on spaces
The variable is not quoted when used. Always use double quotes around variables: echo "$VAR". Without quotes, Bash performs word splitting on spaces, which can break the value into separate arguments.
$VARtext expands to an empty string
Use braces when appending literal text directly after a variable name. Write "${VAR}text" instead of "$VARtext" so Bash does not treat it as a different variable name. Also verify the variable is set with echo "${VAR}" before concatenating.
Script fails with VAR+=...: not found
The script was started with sh, not bash. On Debian, Ubuntu, and their derivatives, /bin/sh points to dash. Dash is a POSIX shell and has no += operator. Running sh concat.sh ignores the #!/bin/bash shebang. Dash then reads the assignment as a command name:
concat.sh: 3: VAR+= World: not foundRun the script with bash concat.sh, or make it executable and start it with ./concat.sh. Dash also rejects printf -v with printf: Illegal option -v. If your script must run under sh, use juxtaposition instead: VAR="${VAR} World". For the other ways to start a script, see our guide on running Bash scripts
.
FAQ
How do I concatenate strings with a custom separator?
Use printf -v: printf -v RESULT '%s-%s' "$VAR1" "$VAR2" produces value1-value2. For a comma-separated list built in a loop, append ${ELEMENT}, inside the loop and trim the trailing comma afterwards.
How do I include a newline between two strings?
Use $'\n' as the separator: VAR3="$VAR1"$'\n'"$VAR2". Alternatively, use printf -v VAR3 '%s\n%s' "$VAR1" "$VAR2".
Can I concatenate command output with a string?
Yes. Use command substitution: VAR="Today is $(date +%F)". The output of the command replaces $(...) and is joined with the surrounding string.
What is the difference between double and single quotes in concatenation?
Double quotes allow variable expansion and command substitution. Single quotes treat everything literally. "$VAR World" expands $VAR, while '$VAR World' outputs the literal text $VAR World.
Conclusion
Bash offers three main ways to concatenate strings: placing variables side by side, using the += operator to append, and using printf -v for formatted joins. If you need to test strings for equality or patterns, see our guide on comparing strings in Bash
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page