Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `if $(true) ; then ... fi` succeed?

Tags:

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 
like image 226
Keith Thompson Avatar asked Jan 22 '12 23:01

Keith Thompson


People also ask

What does fi mean in shell script?

fi statement is the fundamental control statement that allows Shell to make decisions and execute statements conditionally.

What does fi do in bash?

fi closes the if statement, while ;; closes the current entry in the case statement. Show activity on this post. The fi is to close the if-block in the y) case statement and the ;; is used to end the y) case.

What is the meaning of if true?

This looks like a debugging statement left in. if(true) This is always true, so you could remove the test entirely and just run with the true statement. Follow this answer to receive notifications.


1 Answers

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.

like image 161
William Pursell Avatar answered Sep 22 '22 19:09

William Pursell