In some scripts, I've seen if
conditional statements written as:
if [ some_conditional_statement ]; then
# do stuff
fi
or
[ some_conditional_statement ] && #do stuff
Is the second example equivalent (do stuff portion will only execute if the conditional is true), but only useful when you only need to perform one command such as:
[ -f "/path/to/some/file" ] && . /path/to/some/file
Or is there another difference I am missing?
&&
. – John WH Smith Oct 01 '14 at 17:16&&
operation already, in the second example, the first part is evaluated, and if true, the second part (right side of the&&
is evaluated, which executes the code), if false, being a logical AND operation, it shorts and skips evaluation of the right side. This appears, to me, to be equivalent of the first example using theif then
syntax, but has the downside of only being able to evaluate one command instead of multiples inside theif
block. Then again, I suppose you could chain&&
's together... Am I missing anything? – SnakeDoc Oct 01 '14 at 17:27&&
andif command; then
. – peterph Oct 06 '14 at 21:43