← Knowledge Base
BashReference note1 min read · updated 2026-11-27

Bash variables and quoting

Assign and expand variables, why you should quote them, and the difference between single and double quotes.

bashquotingvariables
name="George"            # no spaces around =
echo "Hello $name"       # double quotes: variables are expanded
echo 'Hello $name'       # single quotes: literal text
echo "${name}_backup"    # braces when text follows the name
today=$(date +%F)        # command substitution
files=(a.txt "b c.txt")  # array
echo "${files[1]}"       # b c.txt
echo "${#files[@]}"      # number of elements

Always quote expansions: rm "$file", not rm $file. Unquoted, a value with spaces splits into several words and a * expands into file names. Defaults: ${var:-fallback}; fail if unset: ${var:?message}.