23

Inspired by this question:

What should an if statement do when the condition is a command substitution where the command produces no output?

NOTE: The example is if $(true); then ..., not if true ; then ...

For example, given:

if $(true) ; then echo yes ; else echo no ; fi

I would think that $(true) should be replaced by the output of the true command, which is nothing. It should then be equivalent to either this:

if "" ; then echo yes ; else echo no ; fi

which prints no because there is no command whose name is the empty string, or to this:

if ; then echo yes ; else echo no ; fi

which is a syntax error.

But experiment shows that if the command produces no output, the if statement treats it as true or false depending on the status of the command, rather than its output.

Here's a script that demonstrates the behavior:

#!/bin/bash

echo -n 'true:          ' ; if true          ; then echo yes ; else echo no ; fi
echo -n 'false:         ' ; if false         ; then echo yes ; else echo no ; fi
echo -n '$(echo true):  ' ; if $(echo true)  ; then echo yes ; else echo no ; fi
echo -n '$(echo false): ' ; if $(echo false) ; then echo yes ; else echo no ; fi
echo -n '$(true):       ' ; if $(true)       ; then echo yes ; else echo no ; fi
echo -n '$(false):      ' ; if $(false)      ; then echo yes ; else echo no ; fi
echo -n '"":            ' ; if ""            ; then echo yes ; else echo no ; fi
echo -n '(nothing):     ' ; if               ; then echo yes ; else echo no ; fi

and here's the output I get (Ubuntu 11.04, bash 4.2.8):

true:          yes
false:         no
$(echo true):  yes
$(echo false): no
$(true):       yes
$(false):      no
"":            ./foo.bash: line 9: : command not found
no
./foo.bash: line 10: syntax error near unexpected token `;'
./foo.bash: line 10: `echo -n '(nothing):     ' ; if               ; then echo yes ; else echo no ; fi'

The first four lines behave as I'd expect; the $(true) and $(false) lines are surprising.

Further experiment (not shown here) indicates that if the command between $( and ) produces output, its exit status doesn't affect the behavior of the if.

I see similar behavior (but different error messages in some cases) with bash, ksh, zsh, ash, and dash.

I see nothing in the bash documentation, or in the POSIX "Shell Command Language" specification, to explain this.

(Or perhaps I'm missing something obvious.)

EDIT : In light of the accepted answer, here's another example of the behavior:

command='' ; if $command ; then echo yes ; else echo no ; fi

or, equivalently:

command=   ; if $command ; then echo yes ; else echo no ; fi
Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • This is very interesting. Anyway, I think there's an error in your question. You said that `if "" ; then echo yes ; else echo no ; fi` returns no because 'there is no command whose name is the empty string', but by that same logic it should fail with an error something like ": command not found". There is some other difference between `if "" ; then echo yes ; else echo no ; fi` and `if "nonexistant" ; then echo yes ; else echo no ; fi` – Aaron McDaid Jan 23 '12 at 00:21
  • @AaronMcDaid: I think there’s an error in your comment.  `if "nonexistent" ; then …` produces the error message `bash: nonexistent: command not found` and then says `no`.  By comparison, `if "" ; then …` produces the error message `bash: : command not found` and then says `no`.  That’s exactly the same behavior, except `nonexistent` has been deleted (i.e., replaced by *nothing*).  It is the same message that you get if you type just `""` (followed by ).  Can you give an example of a case where bash says something like `` instead of just writing an empty string?  … (Cont’d) – G-Man Says 'Reinstate Monica' Feb 07 '17 at 22:56
  • (Cont’d) …  Can you identify any actual difference between `if "" ; then …` and `if "nonexistent" ; then …`? – G-Man Says 'Reinstate Monica' Feb 07 '17 at 22:56
  • @KeithThompson: I agree that this is an interesting question.  With all the illuminating examples presented on this page, I am surprised that nobody has offered `if "$(true)" ; then …` and `command='' ; if "$command" ; then …` (with quotes), which do the same as ``if "" ; then …``. – G-Man Says 'Reinstate Monica' Feb 07 '17 at 22:57

5 Answers5

15

See section 2.9.1 of the language spec. The last sentence of the first section reads:

If there is a command name, execution shall continue as described in Command Search and Execution . If there is no command name, but the command contained a command substitution, the command shall complete with the exit status of the last command substitution performed. Otherwise, the command shall complete with a zero exit status.

The $(true) is expanding to the empty string. The shell parses the empty string and finds that no command is given and follows the above rule.

JJJ
  • 32,902
  • 20
  • 89
  • 102
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Excellent! That's easy to miss (I did). – Keith Thompson Jan 23 '12 at 17:17
  • Hey, I was right! Thanks for finding the pertinent section in the spec – SiegeX Jan 24 '12 at 00:40
  • Is there a guarantee in the standard that "if $(true) $(false)" will evaluate the false last? – William Pursell Jan 24 '12 at 14:13
  • 1
    @WilliamPursell: You asked, “Is there a guarantee in the standard that `if $(true) $(false)` will evaluate the `false` last?” The answer lies in the second sentence of [Section 2.9.1](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01): “When a given simple command is required to be executed …, the following expansions, **assignments**, and redirections shall all be performed **from the beginning of the command text to the end**” (emphasis added). – G-Man Says 'Reinstate Monica' Feb 07 '17 at 22:55
6

There are potentially two exit codes to consider. First, here's another two experiments that should help:

# if $(echo true; false) ; then echo yes ; else echo no ; fi
yes

The inner command exits with failure because of the false. But that's irrelevant because the output of the command is non-empty and hence the output ("true") is executed instead and its exit code takes priority.

# if $(echo false; true) ; then echo yes ; else echo no ; fi
no

Again, the command line inside the $( ) is successful, but the output is no because the output ("false") takes priority.

The exit status of the commands inside the $( ) is relevant if and only if the output is empty or only whitespace. If the output is empty, there is no list of commands to execute and hence it appears that the shell will fall back on the exit status of the inner command.

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
  • 1
    It's not so much a case of priority but the fact that bash only keeps one exit code and its value is the exit code of the last executed command. `$(true)` and `$(false)` are sub'd for null commands which does not count towards execution and thus the exit code of `true` and/or `false` remain. – SiegeX Jan 23 '12 at 00:37
  • "The exit status of the commands inside the $( ) is relevant *if and only if* the output is empty." That appears to be the case, but *why?* Can you point to any documentation from which you could infer that behavior? – Keith Thompson Jan 23 '12 at 01:04
  • @KeithThompson, I don't know any such documentation. I was tempted to refer to this as an 'undocumented' feature :-) As @SiegeX said, I think that it uses the exit status of the last command that was actually executed, regardless of in what 'context' that command was executed. i.e. Inside or outside `$( )`, what was the last command executed? – Aaron McDaid Jan 23 '12 at 01:09
  • 1
    “The exit status of the commands inside the $( ) is relevant if and only if the output is empty”: not exactly; it's enough that the output be all whitespace. See [William Pursell's answer](http://stackoverflow.com/questions/8965887/why-does-if-true-then-fi-succeed/8973170#8973170). – Gilles 'SO- stop being evil' Jan 24 '12 at 00:22
5

What appears to be happening is that bash keeps the exit status of the last executed command

This would explain why $(true) and $(false) have different behavior in an if test. They both produce null commands which doesn't count as execution but they have different exit codes.

As soon as you use command substitution on a command that has output, $() attempts to execute that output as a command and the exit code of that attempt is now the latest one used for the if test

SiegeX
  • 135,741
  • 24
  • 144
  • 154
3

What should an if statement do when the condition is a command substitution where the command produces no output?

Output doesn't matter. What matters is the exit code:

   if list; then list; [ elif list; then list; ] ... [ else
   list; ] fi
          The if list is executed.  If its exit status is zero,
          the then list is executed.  Otherwise, each elif list
          is executed in turn, and if its exit status is zero,
          the corresponding then list is executed and the
          command completes.  Otherwise, the else list is
          executed, if present.  The exit status is the exit
          status of the last command executed, or zero if no
          condition tested true.

If you replace your $(echo true) and $(echo false) with something else you will probably see what is going on:

$ if $(echo false) ; then echo yes ; else echo no ; fi
no
$ if $(echo command-does-not-exist) ; then echo yes ; else echo no ; fi
command-does-not-exist: command not found
no
$ 

The $(..) runs the command and then if executes the results (in this case, just true or false or does-not-exist). An empty $() starts a subshell which successfully runs to completion and returns an exit code of 0:

$ if $() ; then echo yes ; else echo no ; fi
yes

Aaron raised some interesting points:

$ if $(echo true; false) ; then echo yes ; else echo no ; fi
yes
$ if $(echo 'true ; false') ; then echo yes ; else echo no ; fi
yes
$ if true ; false ; then echo yes ; else echo no ; fi
no

$ if $(echo false ; true) ; then echo yes ; else echo no ; fi
no
$ if $(echo 'false ; true') ; then echo yes ; else echo no ; fi
no
$ if false ; true ; then echo yes ; else echo no ; fi
yes
$ 

It appears that when executing a command constructed via the $() subshell, the first command's exit status is what matters.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 1
    The exit code of the command inside the `$( )` doesn't matter if the output is non-empty. `if $(echo true; false) ; then echo yes ; else echo no ; fi` gives `yes`. So the `false` is ignored because the *outputed* command ("true") is executed instead. – Aaron McDaid Jan 23 '12 at 00:25
  • @Aaron, that's a cool example. I had to play with it a bit further and now I'm more confused than when I started. :) – sarnold Jan 23 '12 at 00:34
  • *It appears that when executing a command constructed via the $() subshell, the first command's exit status is what matters.* If that were the case then the following should produce `no`: `if $(false;echo 'true ; false') ; then echo yes ; else echo no ; fi` – SiegeX Jan 23 '12 at 00:45
  • @SiegeX: but the first command constructed via the `$()` subshell in your example is `true`. Don't forget that `if` _executes_ the results of the output of the `$()` ... (If you can suggest better phrasing, please do. I went around on circles on that specific sentence for five minutes before settling on that.) – sarnold Jan 23 '12 at 00:50
  • *"An empty `$()` starts a subshell which successfully runs to completion and returns an exit code of `0`"* -- That does appear to be the case; the question is **why?** Where is this behavior documented? – Keith Thompson Jan 23 '12 at 01:40
  • 3
    @sarnold So after looking at it a bit more, the reason why `if $(echo 'true ; false'); then ...` and `if true ; false; then ...` give different results is because command substitution only undergoes word splitting and glob expansion, nothing more. Therefore `$(echo 'true ; false')` expands to `true \; false`, i.e. `true` is called with two arguments: `;` and `false` and not two separate commands – SiegeX Jan 23 '12 at 05:18
  • Perhaps this should be considered a special case of "if foo=$(cmd)", in which no variable assignments are being made. – William Pursell Jan 23 '12 at 14:22
  • 2
    @SiegeX’s explanation can be further demonstrated by `if $(echo 'ls -l false ; true') ; then …`, which outputs `false: No such file`, `;: No such file`, and `true: No such file` (unless you have files by those names), and then `no`.  This shows that the `;` is interpreted as an argument and not a command separator.  On the other hand, `if $(echo 'eval false ; true') ; then …` outputs `yes`, because the `eval` causes the shell to interpret the `;` as a separator, and to execute `false` *and then* `true`.  (But don’t get in the habit of using `eval`; to the contrary, you should try to avoid it.) – G-Man Says 'Reinstate Monica' Feb 07 '17 at 22:55
0

To add what a lot of people are probably trying to determine when looking for the reasons for this behavior:

If you want to strictly evaluate if a variable is boolean true, evaluating to false in all cases other than boolean true), use:

if [[ ${subject} = true ]]; then { ... } fi

If you want to strictly evaluate if a variable is boolean false, use:

if [[ ${subject} = false ]]; then { ... } fi
Peter Kionga-Kamau
  • 6,504
  • 2
  • 17
  • 13