Skip to content
Vidar Holen edited this page Apr 8, 2025 · 1 revision

[ ! -o opt ] is always true because -o becomes logical OR. Use or ! [ -o opt ].

Or "[ ! -a file ] is always true because -a becomes logical AND. Use -e instead."

Problematic code:

if [ ! -o braceexpand ]
then 
  ..
fi

Correct code:

if [[ ! -o braceexpand ]]
then 
  ..
fi

or

if ! [ -o braceexpand ]
then 
  ..
fi

Rationale:

Bash interprets [ ! -o opt ] as [ "!" ] || [ "opt" ] instead of negating the condition. As a result, the condition is always true.

Avoid this by using [[ ! -o opt ]] or ! [ -o opt ].

The same issue applies to [ ! -a file ], but this is easier fixed using POSIX standard [ ! -e file ].

Exceptions:

None.

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

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