Skip to content

Files

Latest commit

 

History

History
36 lines (23 loc) · 730 Bytes

SC2015.md

File metadata and controls

36 lines (23 loc) · 730 Bytes

Pattern: Use of confusing A && B || C in shell script

Issue: -

Description

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

Exceptions

Ignore this warning when you actually do intend to run C when either A or B fails.

Further Reading