Skip to content
Vidar Holen edited this page Aug 27, 2021 · 11 revisions

Expressions don't expand in single quotes, use double quotes for that.

Problematic code:

name=World
echo 'Hello $name'   # Outputs Hello $name

Correct code:

name=World
echo "Hello $name"   # Outputs Hello World

Rationale:

ShellCheck found an expansion like $var, $(cmd), or `cmd` in single quotes.

Single quotes express all such expansions. If you want the expression to expand, use double quotes instead.

If switching to double quotes would require excessive escaping of other metacharacters, note that you can mix and match quotes in the same shell word:

dialog --msgbox "Filename $file may not contain any of: "'`&;"\#%$' 10 70

Exceptions

If you know that you want the expression literally without expansion, you can ignore this message:

# We want this to output $PATH without expansion
# shellcheck disable=SC2016
echo 'PATH=$PATH:/usr/local/bin' >> ~/.bashrc

ShellCheck also does not warn about escaped expansions in double quotes:

echo "PATH=\$PATH:/usr/local/bin" >> ~/.bashrc

Related resources:

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally