Skip to content
Vidar Holen edited this page Jan 21, 2015 · 7 revisions

Note that ~ doesn't expand in quotes.

Problematic code:

rm "~/Desktop/$filename"

Correct code:

rm ~/"Desktop/$filename"

Rationale:

Tilde does not expand to the user's home directory when it's double quoted.

To expand it, the tilde, the optional username, and its following slash should be outside of the double quotes.

These strings expand:

~/file                    # Correct: tilde up to the slash is unquoted (everything is)
~/"$var"                  # Correct: tilde up to the slash is unquoted
~user/"hello world.txt"   # Correct: tilde up to the slash is unquoted

These strings do not, leaving a literal tilde:

# Fails!
"~/file"                  # Wrong: tilde and slash are quoted when they shouldn't be.
~"/file"                  # Wrong: slash is quoted when it shouldn't be.
~user"/hello world.txt"   # Wrong: slash is quoted when it shouldn't be.

Exceptions

If you don't want the tilde to be expanded, you can ignore this message.

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