The shell itself only runs the command and evaluates its exit code. A zero exit code signifies success; all other values indicate failure.
if command; then
: things to do if the exit code from command was 0
else
: things to do if it was not 0
fi
while command; do
: things to do if the exit code was 0
done
The command [
(aka test
) is very commonly used in conditionals, because the original Bourne shell lacked built-in operators to check if a string was empty or a file existed. Modern shells have this command built in, and many shells have an extended and modernized version [[
, but this is not properly portable to POSIX sh
and should thus be avoided for portable scripts. This related question explains the differences between the two in more detail.
The notation (( ... ))
introduces an arithmetic context. Again, this was something which was not part of the original Bourne shell (it had a dedicated external tool expr
for these things) but modern shells have this built in. The result code of an arithmetic expression is 0 if the result of the arithmetic evaluation was not 0 (or an error).
The notation ( command )
creates a subshell and evaluates command
in that. There are situations where this is actually necessary and useful, but if you are only just learning the syntax, you are unlikely to need this.
... In fact, in the majority of scripts I have seen this used in a conditional, it was clearly unnecessary.
Another antipattern to look out for is
command
if [ $? = 0 ]; then
: things
fi
You should almost never need to examine $?
explicitly, and in particular, comparing it to zero is something if
and while
specifically do for you behind the scenes. This should simply be refactored to
if command; then
: ...