~ does not expand inside quotes
SC2088: `"~/file"` is the literal 6 characters, not your home directory. Use `$HOME/file` or unquoted `~`.
Problem
Tilde expansion happens only on an unquoted, unescaped leading `~`. Once inside quotes it is a literal character, which usually leaves you writing to a file named `~` in the current directory.
Bad
cp config "~/.myapp/config"Good
cp config "$HOME/.myapp/config"Explanation
`$HOME` works inside quotes and is easier to review. Reserve bare `~` for command-line convenience, not scripts.
When It Matters
Tilde expansion happens before quote removal, and only on an unquoted tilde at the start of a word. The moment you write "~/backups" the shell hands the literal seven characters to the command, and you get a directory named "~" created in the current working directory instead of one under your home. This bites hardest in backup and installer scripts, where the mistake silently creates a stray ~ directory that nobody notices until it turns up in a repository listing months later. The failure is quiet because most commands happily accept the literal path: mkdir creates it, cp copies into it, and only a later "cd ~/backups" — unquoted, so expanded properly — reveals that the real home directory never received anything.
Second Example
Note
Inside a variable assignment the value is later expanded from the variable, and tilde expansion does not run on variable contents at all — so $HOME is the only reliable form once the path lives in a variable.
Exceptions
A tilde that is genuinely meant to be literal — a filename that really starts with ~, an editor backup file such as notes.txt~, or a regex — is fine quoted, and that is exactly when you should quote it. If ShellCheck still flags a line you know is correct, add "# shellcheck disable=SC2088" directly above it with a short comment explaining why the literal tilde is intended.
Faq
Q
Why does ~/path work on the command line but not in my script?
A
It works in both, as long as the tilde is unquoted and at the start of the word. What differs is that scripts more often build paths in variables, and tilde expansion never applies to the contents of a variable — only to the literal text the shell parses.
Q
Is $HOME always safer than ~?
A
It is more predictable: $HOME is an ordinary variable expansion, so it works inside double quotes, inside variable assignments, and after other characters in a word. The tilde is only expanded in a narrow set of unquoted positions.
Q
How do I refer to another user’s home directory?
A
Unquoted ~username expands to that account’s home directory from the password database. There is no variable equivalent, so if you need it inside a quoted string, capture it first with home=$(eval echo "~$user") — or better, read it from getent passwd to avoid eval entirely.