Pattern: Use of confusing A && B || C
in shell script
Issue: -
It's common to use A && B
to run B
when A
is true, and A || C
to run C
when A
is false.
However, combining them into A && B || C
is not the same as if A then B else C
.
In this case, if A
is true but B
is false, C
will run.
Example of incorrect code:
[[ $dryrun ]] && echo "Would delete file" || rm file
Example of correct code:
if [[ $dryrun ]]
then
echo "Would delete file"
else
rm file
fi
Ignore this warning when you actually do intend to run C when either A or B fails.