I am writing a bash script which contains a simple if section with two conditions:
if [[ -n $VAR_A ]] && [[ -n $VAR_B ]]; then
echo >&2 "error: cannot use MODE B in MODE A" && exit 1
fi
A senior engineer reviewed my code and commented:
please avoid using && when you could simply execute the two commands in subsequent lines instead.
He didn't further explain. But out of curiosity, I wonder if this is true, and what is the reason for avoiding using &&
.
Best Answer
The review comment probably refers to the second usage of the
&&
operator. You don't want to not exit if theecho
fails, I guess, so writing the commands on separate lines makes more sense:BTW, in bash you can include
&&
inside the[[ ... ]]
conditions: