Concatenate Strings in Bash

To concatenate strings in Bash, place variables and literal text next to each other, or append text to an existing variable with +=. Bash does not need a string concatenation operator like +. Adjacent parameter expansion is enough.

The examples below show the common ways to join Bash string variables, add spaces between strings, concatenate strings inside another string, and build a result inside a loop.

Bash string concatenation syntax

The safest general pattern is to wrap variable names in braces when they are placed next to more characters. This avoids ambiguity when Bash tries to read the variable name.

</>
Copy
result="${first}${separator}${second}"

For example, if first is Learn, separator is a space, and second is Bash, the result becomes Learn Bash.

Technique 1 – Using +=   : Append to variable

Appending str2 to str1. [str1 = str1 + str2]

</>
Copy
~$ str1="Learn"
~$ str2=" Bash Scripting"
~$ str1+=$str2
~$ echo $str1
Learn Bash Scripting

The += form changes the value of the variable on the left side. Use it when you want to keep adding text to the same variable, such as while building a message, a file name, or a comma-separated string.

</>
Copy
message="Status:"
message+=" completed"
printf '%s\n' "$message"
Status: completed

Technique 2 – Keep two string variables side by side

</>
Copy
~$ str1="Learn"
~$ str2=" Bash Scripting"
~$ str3=$str1$str2
~$ echo $str3
Learn Bash Scripting

This is the most direct Bash concatenation style. Bash expands $str1 and $str2, then treats the expanded values as one continuous string in the assignment.

When a variable is followed by letters, digits, or underscores, use braces so Bash knows where the variable name ends.

</>
Copy
prefix="report"
file_name="${prefix}_2026.txt"

printf '%s\n' "$file_name"
report_2026.txt

Without braces, $prefix_2026 would be read as one variable name. If that variable is not set, the output may be empty or unexpected.

Technique 3 – Use a string variable in another string

~$ str1="Learn"
~$ str2="$str1 Bash Scripting"
~$ echo $str2
Learn Bash Scripting

Double quotes allow variables to expand inside the string. Single quotes do not expand variables, so use double quotes when the final string should include the value of a Bash variable.

</>
Copy
name="Bash"

double_quoted="Learn $name"
single_quoted='Learn $name'

printf '%s\n' "$double_quoted"
printf '%s\n' "$single_quoted"
Learn Bash
Learn $name

Concatenate Bash strings with literal text

You can mix variables and fixed text in one assignment. This is common when creating file paths, labels, log messages, and option values.

</>
Copy
directory="/home/user"
file="notes.txt"

path="${directory}/${file}"
printf '%s\n' "$path"
/home/user/notes.txt

Quoting the final variable in printf keeps spaces and special characters in the value from being split by the shell.

Concatenate strings in a Bash loop

For loops often use += to build a longer string one part at a time. Add the separator conditionally so the final value does not start with an unwanted space or comma.

</>
Copy
words=("Bash" "string" "concatenation")

sentence=""
for word in "${words[@]}"; do
    if [ -z "$sentence" ]; then
        sentence="$word"
    else
        sentence+=" $word"
    fi
done

printf '%s\n' "$sentence"
Bash string concatenation

This pattern is useful when the number of parts is not fixed. For large lists, arrays are often easier to manage than repeatedly building one string, but += is simple and readable for short strings.

Bash string concatenation when building commands

Do not build a whole command by concatenating one long string and then executing it. Spaces, quotes, and special characters can make the command behave differently from what you intended. Prefer arrays for command arguments.

</>
Copy
pattern="error message"
file="app.log"

grep_args=(-i "$pattern" "$file")
grep "${grep_args[@]}"

String concatenation is fine for values such as paths and labels. For executable commands with separate arguments, Bash arrays keep each argument separate.

Common Bash concatenate strings mistakes

  • Using + as a string operator: str3=$str1+$str2 includes the plus sign as literal text. Bash string concatenation is normally done by placing values side by side.
  • Forgetting braces near extra characters: use ${name}_log instead of $name_log when _log is fixed text.
  • Using single quotes when expansion is needed: '$name' stays as literal text, while "$name" expands the variable.
  • Printing unquoted variables: prefer printf '%s\n' "$result" when the value may contain spaces, tabs, or wildcard characters.

FAQ on Bash concatenate strings

Can I concatenate strings in Bash with the plus operator?

No. Bash does not use + as a string concatenation operator. If you write result=$a+$b, the plus sign becomes part of the assigned string. Use result="$a$b" or result="${a}${b}" instead.

When should I use braces while concatenating Bash variables?

Use braces when a variable is directly followed by characters that could be read as part of the variable name. For example, use "${prefix}_file" instead of "$prefix_file".

Does += work in every shell script?

+= is a Bash feature for appending to variables. If a script must run in a strictly POSIX sh shell, use a normal assignment such as var="${var}${next}".

How do I concatenate Bash strings with a space between them?

Place the space inside the quoted assignment: result="${first} ${second}". The space is literal text between the two variable values.

How do I concatenate strings repeatedly in a Bash loop?

Initialize an empty variable before the loop and append each part with +=. Add separators only after checking whether the result is still empty, so the final string does not start with an extra separator.

Editorial QA checklist for Bash string concatenation examples

  • Each Bash concatenate strings example shows whether it modifies an existing variable or creates a new variable.
  • Examples that combine variables with suffixes or prefixes use braces, such as ${prefix}_file.
  • Output-only examples use the output class, and new Bash examples use the language-bash class.
  • Explanations distinguish string values from command arguments, especially when discussing command construction.
  • The tutorial reminds readers that single quotes prevent Bash variable expansion.

Bash string concatenation summary

In Bash, concatenate strings by placing variable values and literal text next to each other, by using += to append to an existing variable, or by expanding a variable inside double quotes. Use braces for clarity when text immediately follows a variable name, and quote variables when printing or passing the final value to another command.

For more Bash basics, continue with this Bash Tutorial. For reference syntax, see the GNU Bash manual on shell parameter expansion.