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

情到浓时终转凉″ 提交于 2019-11-28 23:05:55
William Pursell

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!