Web lists-archives.com

Challenge: a VERY strange problem with command substitution in bash




Hello,
  this is my first posting here as I do not see any other hope of
getting this resolved. Research in mailing lists and other Cygwin users'
questions on various sites have not proven to be useful.

1. Ubuntu 4.4.0-71-generic x86_64, /bin/bash

$ value="$( date | cat )"; echo "$? <$value>"
0 <Tue Jul 11 20:21:22 CEST 2017>
$ value="$( date )"; echo "$? <$value>"
0 <Tue Jul 11 20:21:59 CEST 2017>
$ date | cat
Tue Jul 11 20:22:22 CEST 2017

Results are the same for normal users and for root. Everything is fine
and as expected.

2. Windows 10, Version 1703, Build 15063.413 on a Dell 64bit platform,
latest 64bit Cygwin (CYGWIN_NT-10.0, 2.8.1(0.312/5/3) 2017-07-03 14:11
x86_64 Cygwin) /bin/bash

$ value="$( date | cat )"; echo "$? <$value>"
127 <>
$ value="$( date )"; echo "$? <$value>"
0 <Tue Jul 11 20:25:01 CEST 2017>
$ date | cat
Tue Jul 11 20:25:17 CEST 2017

Results are the same for normal users and for users with administrative
rights, even if I run this under the TrustedInstaller rights.
Fully-qualified paths (/bin/date and /bin/cat) does not change the results.

3. On the same Windows 10 system as above, the following forms work fine

$ value="$( date | cat )"; echo "$? <$value>"
0 <Tue Jul 11 20:26:12 CEST 2017>
$ value="$( bash -c 'date | cat' )"; echo "$? <$value>"
0 <Tue Jul 11 20:25:29 CEST 2017>
# value="$( date ; pwd )"; echo "$? <$value>"
0 <Tue Jul 11 20:28:08 CEST 2017
/home/juergen>


Using backquotes instead of the command substitution with $(...) does
not change the results. I could swear this did work in an earlier
version of Cygwin on my Windows 7 machine.

I tried this to see if the code in the parentheses is executed at all:

$ value="$( date 2> foo | cat )"; echo "$? <$value>"

The file "foo" was not created, i.e., it seems the commands don't really
get executed.

$ value="$( date && pwd )"; echo "$? <$value>"
0 <Tue Jul 11 20:49:09 CEST 2017
/home/juergen>

$ value="$( date || pwd )"; echo "$? <$value>"
0 <Tue Jul 11 20:32:27 CEST 2017>

both work, so some control structures seem to be permissible... just not
a pipe.

What is going on? Some misconfiguration? A Cygwin bug? Some interaction
with something weird in Windows 10? I am at loss to understand what
could be wrong... and am now most curious whether anybody has an idea
what is causing this. Does it work/not work in the same way in your
Cygwin installation?

I came across this effect because ssh-host-config did not recognize me
as administrator anymore. It's due to a check for a certain user group
that uses a command substitution with a pipe. Replacing this with an
equivalent command works. The original line used "id -G" and then a
"grep -Eq" to check whether a certain group is on that list.

I am VERY curious now! I've rarely been puzzled that much by such a very
elementary shell expression (looking back at 34 years of Unix experience).

Thanks much in advance!
--Jürgen


Attachment: smime.p7s
Description: S/MIME Cryptographic Signature