koalaman / shellcheck Public
SC2016
Vidar Holen edited this page Aug 27, 2021
·
6 revisions
Expressions don't expand in single quotes, use double quotes for that.
Problematic code:
name=World
echo 'Hello $name' # Outputs Hello $nameCorrect code:
name=World
echo "Hello $name" # Outputs Hello WorldRationale:
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 70Exceptions
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:
- StackOverflow: How do I use variables in single quoted strings?