Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What are the three formats of conditional statements used in bash scripting?

share|improve this question
2  
Is this homework? –  intuited Jan 7 '11 at 4:40
    
yes :( i'm not sure if they mean different if statements or are there other things as well –  Jon Doe Jan 7 '11 at 4:49
add comment

1 Answer

up vote 6 down vote accepted

Well, I'm not really sure what the question is asking either, but I think that's not really relevant. As I understand it, the StackExchange take on answering homework-ish questions is that the answers should be generally useful, i.e. should be able to serve as a reference for people who are not just trying to answer a very specific question.

The relevant section of the bash manual lists 5 different constructs which can be used for conditional evaluation. Of these, the if, case, and [[ .. ]] 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.";

&& and || 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;

but works differently: in the first form, the conditional logic really comes about as a side effect of the actual explicit goal of the code, which is, at least ostensibly, to evaluate the logical operators. This sort of construct is also commonly seen in C and JavaScript code.

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 in 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

echo $?;

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 $?
0
$ (( 1 + 2 == 4 )); echo $?
1

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 case statements.

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.