Concatenate Variables to Strings in Bash
In Bash, strings and variable values can be joined by placing them next to each other. Bash does not require a dedicated concatenation operator such as +. You can combine literal text, variables, separators, newlines, and command output within the same assignment or quoted string.
This tutorial explains how to concatenate Bash variables with strings, join multiple variables, avoid unwanted spaces, use braces around variable names, add separators, append text to an existing variable, and include command output.
Include a Bash Variable in a String
A variable can be embedded in a double-quoted string by writing a dollar sign followed by the variable name. Bash replaces the variable reference with its current value.
In the following example, the value stored in n1 is included in the text passed to echo.
Bash Script
#!/bin/bash
n1=10
echo "Number of Apples : $n1"
Output
Number of Apples : 10
Double quotes allow variable expansion. Single quotes do not expand variables, so echo 'Number of Apples : $n1' would print $n1 literally.
Concatenate Two Bash Variables into One String
To concatenate two variables, place their references next to each other inside a double-quoted assignment. No operator is required between them.
In the following example, we use the idea of including variables in a string to concatenate two variables.
Bash Script
#!/bin/bash
n1=10
str1="Number of Apples : "
str1="$str1$n1"
echo $str1
Output
Number of Apples : 10
When displaying a concatenated value, quote the variable as echo "$str1". Quoting prevents Bash from performing unwanted word splitting or pathname expansion when the value contains spaces or wildcard characters.
Concatenate a Variable and Text without a Space
Bash does not insert spaces automatically during concatenation. Place the variable and literal text next to each other to join them without a space.
#!/bin/bash
file_name="report"
extension=".txt"
full_name="$file_name$extension"
echo "$full_name"
Output
report.txt
To include a space, add it explicitly inside the quoted string, such as full_name="$first_name $last_name".
Use Braces When Text Follows a Variable Name
Use the ${variable} form when characters immediately after the variable could be interpreted as part of its name. Braces clearly mark where the variable name ends.
#!/bin/bash
user="alex"
backup_file="${user}_backup.tar.gz"
echo "$backup_file"
Output
alex_backup.tar.gz
Without braces, Bash would read $user_backup as a reference to a variable named user_backup, rather than the value of user followed by _backup.
Append Text to an Existing Bash Variable
You can append text by assigning the current value and the new text back to the same variable. Bash also supports the += assignment operator for appending strings.
#!/bin/bash
message="Processing"
message="$message file"
message+=" complete"
echo "$message"
Output
Processing file complete
Spaces inside the appended text are preserved because the assignments are quoted.
Join Bash Strings with a Separator
To join values with a separator, place the separator between the variable references. The separator can be a space, comma, slash, colon, hyphen, or another string.
#!/bin/bash
host="localhost"
port="8080"
address="$host:$port"
year="2026"
month="07"
day="22"
date_value="$year-$month-$day"
echo "$address"
echo "$date_value"
Output
localhost:8080
2026-07-22
Concatenate Strings with a Newline in Bash
For predictable newline handling, use printf. A command substitution can also generate a newline-separated value, although trailing newline characters are removed by command substitution.
#!/bin/bash
first_line="Build started"
second_line="Build completed"
message=$(printf '%s\n%s' "$first_line" "$second_line")
printf '%s\n' "$message"
Output
Build started
Build completed
For simple output, you can avoid storing the combined value and call printf '%s\n%s\n' "$first_line" "$second_line" directly.
Concatenate a String with Command Output
Use command substitution with the $(command) syntax to capture a command’s standard output and include it in a string.
#!/bin/bash
current_user=$(whoami)
message="Current user: $current_user"
echo "$message"
You can also place command substitution directly inside the string:
echo "Current directory: $(pwd)"
Command substitution removes trailing newline characters from the captured output. Internal newlines remain part of the value.
Concatenate an Array of Bash Strings
When values are stored in an array, Bash can join them using the first character of the IFS variable as a separator.
#!/bin/bash
items=("red" "green" "blue")
joined=$(IFS=,; echo "${items[*]}")
echo "$joined"
Output
red,green,blue
The temporary IFS=, setting applies only to the command that follows it. Using ${items[*]} joins the array elements with the selected separator.
Common Bash String Concatenation Mistakes
- Adding spaces around
=: Writename="Alex", notname = "Alex". Spaces cause Bash to interpretnameas a command. - Using single quotes: Variables inside single quotes are not expanded. Use double quotes when the string must include variable values.
- Omitting braces before adjacent text: Prefer
${name}_filewhen text immediately follows a variable reference. - Leaving expansions unquoted: Use
"$value"to preserve spaces and prevent pathname expansion. - Expecting
+to concatenate strings: In a normal Bash assignment, adjacent values are concatenated without a plus operator.
Bash Variable Concatenation FAQs
How do I concatenate a Bash variable and a string without a space?
Place them next to each other inside double quotes, as in result="${name}.txt". Bash adds no space unless one is explicitly included.
Why should I use braces when concatenating Bash variables?
Braces separate the variable name from adjacent characters. For example, ${user}_home expands the variable named user, while $user_home refers to a different variable named user_home.
Can I concatenate strings in Bash with the plus operator?
A plus operator is not needed for ordinary Bash string concatenation. Use adjacent expansions such as combined="$first$second", or use combined+="$next" to append to an existing variable.
How do I concatenate command output with a Bash string?
Use command substitution: message="Directory: $(pwd)". Bash executes the command and substitutes its standard output into the string.
How do I join multiple Bash strings with a separator?
For a few variables, insert the separator directly, such as path="$directory/$file". For an array, set IFS temporarily and expand the array with ${array[*]}.
Bash Concatenation Editorial QA Checklist
- Confirm every Bash assignment has no spaces around the
=sign. - Check that variable expansions containing text or spaces are enclosed in double quotes.
- Use
${variable}where characters immediately follow a variable name. - Verify examples distinguish intentional separators from concatenation without spaces.
- Confirm command-substitution examples account for the removal of trailing newlines.
Summary of Bash String and Variable Concatenation
Bash concatenates strings by placing literal text and variable expansions next to each other. Double quotes preserve spaces and allow variable expansion, while braces make variable boundaries clear. You can also append with +=, add separators explicitly, join arrays using IFS, and include command output with $(command).
In this Bash Tutorial, we learned how to concatenate variables to Strings in Bash.
TutorialKart.com