Hi,
I don't understand && and || in this context. I thought && is for logical 'AND' and || is for logical 'OR'.
[ ! -z "$var" ] && echo "Not empty" || echo "Empty"
Please help
Thank You
Hi,
I don't understand && and || in this context. I thought && is for logical 'AND' and || is for logical 'OR'.
[ ! -z "$var" ] && echo "Not empty" || echo "Empty"
Please help
Thank You
Yes, it is AND and OR. They connect pipelines and execute them conditionally. man bash
:
So - if the test result is TRUE, execute the first command list, if FALSE, the second. That's why it acts as a short if - then - else version. It works because the shell uses "Short Cut evaluation", which means that as soon as the program can determine that the expression is false No Further Evaluation takes place.
this is very tricky..I like it.
Unfortunatelly this sequence is not a perfect if-then-else replacement. It works as expected in situations like:
$ true && echo true || echo false
true
$ false && echo true || echo false
false
But consider this:
$ true && false || echo false
false
Ouch Allthough the first expression ist true, both the "then" and the "else" part of the sequence is executed, because the "then" part evaluates to false.
[[ true || ! false ]] && echo yay || echo oh no
Absolutely. This might circumvent the trap (not sure if it opens others):
true && { false || :; } || echo bad
@ sea: || ! false
will never be executed.