Skip to content

test-exit-status

Shellstyleno fixdefault: offshell/shellcheck

Reports a test that compares $? with a number, as in [ $? -eq 0 ].

if cmd already tests the status. A separate test of $? is one more line, and $? is changed by any command that runs in between.

To fix, test the command: if cmd; then.

Reported
if [ $? -eq 0 ]; then :; fi
if [ "$?" != 0 ]; then :; fi
[ $x -eq 0 ]
...
[ -a file ]
...
shell:
lint:
rules:
test-exit-status: warn # off (default), warn, error

Enabled by the presets: shell/shellcheck.

Suppress one line with sudsy-disable-next-line test-exit-status in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
shellcheck SC2181 partial Only [ ]/[[ ]] tests that compare $? directly.