7

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?

SnakeDoc
  • 492

2 Answers2

5

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 there must be a ; (or an & or a newline) before the }.

My opinion:

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

  • FWIW, apart from the presentation and clarity issues, there's also the odd difference between if A; then B; C; fi and A && { B; C; } that if the test A fails, the exit status of the whole if is zero (truthy), but with && it's the falsy non-zero status that A actually returned. – ilkkachu Sep 27 '23 at 08:35
4

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 01 '14 at 17:31
  • 3
    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 01 '14 at 17:44
  • 1
    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. –  Oct 01 '14 at 23:11