On Sat, 2018-06-16 at 10:17 +0100, Ian Campbell wrote: > On Sat, 2018-06-16 at 08:36 +0200, Holger Wansing wrote: > > > The original/final lines are a bit strange, though, instead of > having: > > > > > > if $($git foo bar); then … fi > > > > > > I suppose it should only be: > > > > > > if $git foo bar; then … fi > > > > However, with this simplified variant it fails. So I left it as is > for now. > > It seems there is an interesting (and new to me, or at least I'd > never > fully appreciated the behaviour) corner case of the `if $(foo); then` > syntax, which is that if `foo` exits producing no output then its > exit > code is apparently used for the condition. If `foo` does produce > output > then the shell will attempt to execute that and use the resulting > exit > code. > > These just run true or false and take the output:
Should be "These just run true or false and use the exit code". BTW, it's worth mentioning that `true` and `false` here are actually `/bin/{true,false}` i.e. literal commands which return the appropriate exit code that the shell `fork`s and `exec`s. There's no shell syntax magic[*] going on here where `true` and `false` are somehow parsed specially. Ian. [*] technically `true` and `false` might be shell builtins for performance reasons (and it looks like with `dash` `true` is but `false` isn't). However logically they can be treated as external commands without special handling. To be unambiguous you could rerun all the examples using the explicit /bin/true etc versions directly. > $ dash -c 'if true ; then echo YES ; else echo NO ; fi' > YES > $ dash -c 'if false ; then echo YES ; else echo NO ; fi' > NO Ian.