- If any command runs successfully inside if conditional expression then if treats it as true.
if print; then echo "foo"; else echo "bar"; fi
2 There's a command called test
to evaluate conditional expression.
if test $a -ge $b;
then
echo "a is bigger";
else
echo "b is bigger";
fi
see the test
command ? It evaluates the conditional expression and return true / false base on the evaluation.
-
test
is later replaced with[
.
if [ $a -ge $b ];
then
echo "a is big";
else
echo "b is big";
fi
Yes, the command is [
and it starts evaluating the expression until it gets ]
. You can check it yourself with which [
or even man [
. [
is basically another representation of test
command.
- There's some limitations of using
[
ortest
. For example, it can't evaluate&&
,||
. So here comes[[
with improvements.
if [[ $a > $b || $a == $b ]]; then echo "a is big"; else echo "b is big"; fi
You can also read more about the differences between [
and [[
from here.
- There's no ternary operator. But there's a hack ...
[[ $a == $b ]] && echo "Equal" || echo "Not equal"
Top comments (0)