Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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 Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...