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