
What you need to know:
- Bash concatenate strings operations are simple: put two variables or literals next to each other and they join.
- Use the += append operator when you are building a string across several steps.
- printf gives you clean formatting when you want control over spacing and newlines.
- Heredoc handles multiline strings without painful escaping.
- Most bugs come from quoting, spaces, or unset variables, not the concatenation itself.
Joining strings in bash looks odd the first time you see it. There is no + operator. You just stick variables side by side and it works. This guide walks through the main ways to bash concatenate strings, shows common problems, and covers when each method is the right pick.
What Is Bash Scripting
Bash scripting is writing small programs in the Bash shell. It is one of the most common forms of shell scripting on Linux and is used for automation, server management, deployment, and glue code between tools. Bash scripts are plain text files that the shell reads from top to bottom, running each line as a command.
Most Linux servers have Bash installed by default, which is why shell scripting in Bash is often the fastest way to get something done. You write a .sh file, mark it executable, and run it. No compiler. No build step. That makes it a natural choice for tasks where string handling is one piece of a larger job.
When to Use Bash for String Operations
Reach for bash scripting when the strings are small, the logic is simple, and the script needs to run with only the tools already on the system. Bash variables and string operations are fast enough for config generation, log parsing, filename building, and short automation tasks.
If you are doing heavy text processing, a different language is usually cleaner. But for joining a few bash variables into a path or a message, bash is hard to beat.
What Is String Concatenation in Linux
So what is string concatenation? It is the act of joining two or more strings into one. In Bash, a bash string is just a sequence of characters stored in a variable or written as a literal. You concatenate them by placing them next to each other, with no operator between.
first="hello"
second="world"
full="$first $second"
echo "$full" # hello worldThat is the core idea behind almost every method shown below.
Method 1: Variable Juxtaposition
The simplest way to bash concatenate strings is juxtaposition: put the bash variables next to each other inside a new variable or a quoted string.
name="Ada"
greeting="Hello, $name!"
echo "$greeting" # Hello, Ada!No operator. No function. Just order and quoting.
Concatenating Literal Strings
You can bash concatenate strings made up of literals the same way, by writing them inside one pair of quotes or by placing two quoted bash string fragments side by side.
path="/var""/log" # /var/log
message="user: ""$USER"The second form is useful when you want to keep parts of the string visually separate while still joining them.
Using Curly Braces for Variable Guarding
When a bash variable sits next to text that looks like part of its name, wrap it in ${}. This protects the bash string from being parsed wrong.
file="report"
name="${file}_2026.txt" # report_2026.txtWithout the braces, $file_2026 would look like a new variable. Curly braces are the cheapest fix for one of the most common bash bugs.
Method 2: The += Append Operator
The += operator is a bash append string shortcut. It grows a variable in place, which is useful when you want to add to a string without rewriting it. String concatenation bash style with += reads cleanly and runs fast.
log="Started"
log+=" at $(date)"
echo "$log"
# Output: Started at Mon May 11 17:20:00 CEST 2026Each += appends to the existing value. No need to retype the variable name on the right-hand side, and no risk of forgetting a dollar sign in the middle of a long string.
Building Strings Incrementally in Loops
+= shines inside loops. It also works for bash arrays, which is handy when you want to collect items and turn them into one string at the end. String concatenation bash patterns inside loops look like this:
result=""
for item in one two three; do
result+="$item,"
done
echo "${result%,}" # one,two,threeFor list-like data, using bash arrays and then joining with IFS (the Internal Field Separator, the variable Bash uses to decide how to split and join fields) is often cleaner than building a string hop by hop.
parts=(one two three)
IFS=','; joined="${parts[*]}"; unset IFS
echo "$joined" # one,two,threeMethod 3: Concatenating with printf
printf is useful when you want exact control over formatting. It is stricter than echo, which is why many scripts use it for anything printed to logs or passed to another tool. For bash concatenate strings tasks, bash printf also works as a clean joiner.
first="api"
second="contabo"
third="com"
url=$(printf "%s.%s.%s" "$first" "$second" "$third")
echo "$url" # api.contabo.comThe format string defines the shape, and each %s picks up one argument. No quoting surprises, no spacing mistakes.
Method 4: Using Here-Strings and Heredoc
When the string has more than one line, quoting gets painful fast. Bash heredoc solves that. A heredoc lets you write a bash multiline string between two markers and assign it to a variable without escaping every quote. Reach for it when you would otherwise need several += lines or stacked echoes, for example a config file body, an email template, or a multi-line log message.
message=$(cat <<EOF
Hello, $USER.
Your server is ready.
EOF
)
echo "$message"
# Output:
# Hello, yourname.
# Your server is ready.Variables expand inside the block. If you want the raw text with no expansion, quote the opening marker like <<'EOF'. That is the difference between a template that fills in values and a literal block that prints exactly what you typed.
Here is the same pattern with no expansion, useful when you’re writing out something like a config snippet that contains literal dollar signs:
config=$(cat <<'EOF'
host=$SERVER_HOST
port=$SERVER_PORT
EOF
)
echo "$config"
# Output:
# host=$SERVER_HOST
# port=$SERVER_PORTHere-strings are the smaller sibling. They feed a single bash string to a command:
grep "error" <<< "$log"Both patterns keep bash multiline string handling readable.
Bash String Manipulation Techniques
Concatenation is one part of bash string manipulation. Once you have the string, Bash gives you operators for slicing, replacing, and checking length. Combined with bash echo or printf, you can shape almost any output without calling external tools.
Substring Extraction and Replacement
Bash string manipulation uses ${var:offset:length} for substrings and ${var/old/new} for replacement. These work directly on bash variables without needing sed or awk.
text="hello world"
echo "${text:0:5}" # hello
echo "${text/world/bash}" # hello bashAppending to Multiline Strings
You can extend a bash multiline string by appending more lines with += and a leading newline, or by writing a second bash heredoc and joining the two.
block=$(cat <<EOF
Line 1
Line 2
EOF
)
block+=$'\nLine 3'
echo "$block"The $'\n' syntax inserts an actual newline, which is safer than trying to embed one inside double quotes.
Common Bash String Errors and Fixes
Most bash scripting string bugs fall into a short list of patterns. Watch for these and you will avoid most of the common bash errors.
- Forgetting to quote a variable with spaces:
cp $file /tmpbreaks if$filecontains spaces. Usecp "$file" /tmp. - Missing curly braces next to text:
"$name_log"looks for a variable calledname_log. Write"${name}_log". - Using single quotes when you need expansion:
'Hello $name'prints literally. Use double quotes to interpolate. - Empty variables producing empty strings silently: use
set -uto catch unset variables early. - Mixing arrays and strings without care:
${arr[*]}and${arr[@]}behave differently inside quotes.
Most of these look like small stylistic things. They are not. They are where real scripts break.
FAQ: Bash String Concatenation
The simplest way to bash concatenate strings is to place variables and literals next to each other inside double quotes, for example full="$first $second". That works for most cases without any extra operators.
Use the += operator, which is the standard bash append string approach. String concatenation bash style looks like log+=" more text", and it grows the existing variable in place without rewriting it.
In bash scripting, += appends the right-hand value to the existing bash variables on the left. For strings, it adds the new text to the end. For integers declared with declare -i, it performs addition instead.
Use a bash heredoc to build a bash multiline string in one block, then assign it to a variable with $(cat <<EOF ... EOF). Bash heredoc blocks expand variables by default, which makes joining values across many lines easy to read.