Bash String Concatenation: Combine Variables and Strings

By 

Updated on

4 min read

Bash string concatenation examples

One of the most commonly used string operations in Bash is concatenation — joining two or more strings together by placing one after another.

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:

sh
VAR1="Hello,"
VAR2=" World"
VAR3="$VAR1$VAR2"
echo "$VAR3"
output
Hello, World

You can also mix variables with literal strings:

sh
VAR1="Hello,"
VAR2="${VAR1} World"
echo "$VAR2"
output
Hello, World

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

sh
VAR="Hello"
echo "$VARWorld"    # expands $VARWorld, which is empty
echo "${VAR}World"  # expands $VAR, then appends World
output

HelloWorld

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

Bash does not distinguish variable types; variables are treated as integers or strings depending on context. Concatenating a variable that contains only digits works the same way:

sh
VAR1="Hello, "
VAR2=2
VAR3=" Worlds"
VAR4="$VAR1$VAR2$VAR3"
echo "$VAR4"
output
Hello, 2 Worlds

Appending Strings with the += Operator

The += operator appends a string to the end of an existing variable:

sh
VAR1="Hello,"
VAR1+=" World"
echo "$VAR1"
output
Hello, World

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:

languages.shsh
VAR=""
for ELEMENT in 'Hydrogen' 'Helium' 'Lithium' 'Beryllium'; do
  VAR+="${ELEMENT} "
done

echo "$VAR"
output
Hydrogen Helium Lithium Beryllium

Concatenating Strings with printf

The printf command with -v formats and assigns a string directly to a variable without spawning a subshell:

sh
VAR1="Hello,"
VAR2=" World"
printf -v VAR3 '%s%s' "$VAR1" "$VAR2"
echo "$VAR3"
output
Hello, World

This approach is especially useful when you need to include a separator or control the format precisely:

sh
printf -v RESULT '%s, %s' "Hello" "World"
echo "$RESULT"
output
Hello, World

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

MethodExample
JuxtapositionVAR3="$VAR1$VAR2"
Curly brace delimitersVAR3="${VAR1}suffix"
+= operatorVAR1+=" World"
printf -vprintf -v VAR3 '%s%s' "$VAR1" "$VAR2"
With separatorprintf -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.

${VAR}text does not expand as expected 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.

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. For comparing strings in Bash , see the linked guide.

If you have any questions, feel free to leave a comment below.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

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

View author page