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
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 "<empty string>: command not found". There is some other difference betweenif "" ; then echo yes ; else echo no ; fi
andif "nonexistant" ; then echo yes ; else echo no ; fi
– Loriloriaif "nonexistent" ; then …
produces the error messagebash: nonexistent: command not found
and then saysno
. By comparison,if "" ; then …
produces the error messagebash: : command not found
and then saysno
. That’s exactly the same behavior, exceptnonexistent
has been deleted (i.e., replaced by nothing). It is the same message that you get if you type just""
(followed by <Enter>). Can you give an example of a case where bash says something like<empty string>
instead of just writing an empty string? … (Cont’d) – Diaboloif "" ; then …
andif "nonexistent" ; then …
? – Diaboloif "$(true)" ; then …
andcommand='' ; if "$command" ; then …
(with quotes), which do the same asif "" ; then …
. – Diabolo