Skip to content
koalaman edited this page Jan 11, 2016 · 2 revisions

Quote the rhs of = in to prevent glob matching.

Problematic code:

[[ $a = $b ]]

Correct code:

[[ $a = "$b" ]]

Rationale:

When the right-hand side of =, == or != is unquoted in [[ .. ]], it will be treated like a glob.

This has some unexpected consequences like [[ $var = $var ]] being false (for var='[a]'), or [[ $foo = $bar ]] giving a different result from [[ $bar = $foo ]].

The most common intention is to compare one variable to another as strings, in which case the right-hand side must be quoted.

Exceptions:

If you explicitly want to match against a pattern, you can ignore this warning.

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