Skip to content

and-or-is-not-if-else

Shellsuspiciousno fixdefault: offshell/shellcheck

Reports a list a && b || c of three commands, unless c is echo, printf, true, :, return or exit.

It looks like an if/else, but c runs whenever a && b does not succeed, and that includes the case where a succeeds and b fails. A failing b runs both b and c.

To fix, write an if a; then b; else c; fi.

Reported
...
test -f x && echo yes || cleanup
test -f x && echo yes || echo no
[ -n x ] && (a || b)
a && b
...
shell:
lint:
rules:
and-or-is-not-if-else: warn # off (default), warn, error

Enabled by the presets: shell/shellcheck.

Suppress one line with sudsy-disable-next-line and-or-is-not-if-else 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 SC2015 partial Only the plain three-command list; shellcheck exempts a true/: middle command.