The relevant section of the bash manual lists 5 different constructs which can be used for conditional evaluation. Of these, the
[[ .. ]] constructs are probably the most commonly used in real-world code, though the
(( .. )) construct will get used frequently in scripts that do complex counting or other numerical operations.
But they don't mention another very common form used for branching in bash, which is to just execute a command in combination with short-circuit evaluation, for example
grep -q needle haystack.txt && echo "Needle located!" || echo "No dice or needles.";
|| are logical operators: "and" and "or" respectively.
This is effectively the same as
if grep -q needle haystack.txt; then echo "yea"; else echo "nay"; fi;
It works by taking shortcuts: the parts of the expression, e.g.
x && y || z, are evaluated left-to-right. If the left side of a logical and, e.g. the
x && y, evaluates to false,
bash doesn't bother evaluating the
y part because at that point it already knows that
x && y is false.
The converse is true for logical ors:
true || z always evaluates to true, no matter what z turns out to be.
So if the
grep comes out false,
bash can skip that first
echo, because that part of the expression can't possibly be true. So it moves on to the second
echo. On the other hand, if the grep is true, the first
echo is executed (resulting in those words being, well, echoed..). At this point
bash is done, because whatever the value of the right-hand side of the
||, the result of the expression will still be true.
To get a feel for the way bash works with conditionals, it's best to do some experimentation on the command line. The command
will echo the return code of the previous statement. This is an error code, so
0 means true, and a non-zero value means false. It's a bit confusing at first, but you get used to it.
So you can do, for example
$ (( 1 + 2 == 3 )); echo $?
$ (( 1 + 2 == 4 )); echo $?
and perform similar tests with the
[[ .. ]] construct to get a feel for these basic building blocks. Once you've got that done, move on to
if and then
As for the answer to your question, well, I think that's very dependent on the context it was asked in. But getting a good understanding of the various options should help you figure out what they are looking for there.