6

In some scripts, I've seen if conditional statements written as:

if [ some_conditional_statement ]; then
    # do stuff
fi

or

[ some_conditional_statement ] && #do stuff

Is the second example equivalent (do stuff portion will only execute if the conditional is true), but only useful when you only need to perform one command such as:

[ -f "/path/to/some/file" ] && . /path/to/some/file

Or is there another difference I am missing?

  • Side note: you may want to read about short-circuit evaluation to know more about &&. – John WH Smith Oct 1 '14 at 17:16
  • @JohnWHSmith I believe I understand the && operation already, in the second example, the first part is evaluated, and if true, the second part (right side of the && is evaluated, which executes the code), if false, being a logical AND operation, it shorts and skips evaluation of the right side. This appears, to me, to be equivalent of the first example using the if then syntax, but has the downside of only being able to evaluate one command instead of multiples inside the if block. Then again, I suppose you could chain &&'s together... Am I missing anything? – SnakeDoc Oct 1 '14 at 17:27
  • possible duplicate of What are the shell's control and redirection operators? – G-Man Oct 6 '14 at 21:15
  • 1
    I don't think it is an outright dup - it's more about difference between && and if command; then. – peterph Oct 6 '14 at 21:43
3

The if-clause will execute whatever is in between then and fi if the expression after the if is true, which in shell terms mean that its exit value is zero. You can use a command or the brackets which is just a short form for the test command. The a && b construct means: "evaluate a and if its true also evaluate b". So your two examples are equivalent, but the if-clause is more readable and can be used to group several commands.

  • hmm, that is what I had suspected. Comes down to a readability issue depending on what you are doing (in my case, checking if files or directories exist, and if not, making them, so the && construct is more readable than a bunch of cascading if statements). I just wanted to be sure bash wasn't doing anything else with this alternate syntax. – SnakeDoc Oct 1 '14 at 17:31
  • 2
    I would not consider && as an alternate syntax to if, it is really what it looks like, that is "and". You could for instance do: if echo && echo; then echo ok; fi. The reason it can be used as an if-statement is the short circuiting. – megahallon Oct 1 '14 at 17:44
  • Personally, don't feel if adds any readability for simple logic constructs, and prefer it in keeping code short. For longer/more-complicated blocks of code it obviously helps separate out what's getting executed as a result of a condition. It's also worth noting that there are a number of languages where short circuit evaluation is in many cases clearly preferable to if/else constructs, i.e. perl. – BroSlow Oct 1 '14 at 23:11
3

OK, I presume that you understand that [ condition ] (or [[ condition ]] in bash) is, essentially, a command that has no side effects, but that evaluates the condition and gives an appropriate exit status.  You mentioned chaining &&s together.  You can do that;

A  &&  B  &&  C

is equivalent to

if A
then
    if B
    then
        C
    fi
fi

But you seem to be asking how to do

if A
then
    B
    C
fi

You can do that with

A  &&  (B; C)

or

A  &&  { B; C;}

Notes:

  • The parentheses give you a subshell; i.e., commands B and C run in a child process.  Therefore, commands like variable assignments or cd will have no effect on the parent shell.  Commands in braces run in the same process as the A command.
  • In the brace syntax, there must be a space after the { and a ; (or a &) before the }.

My opinion:

I strongly recommend using the if-then-else syntax, for improved readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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