bug with built-in commands in bash when redirecting output

Eric Blake ebb9@byu.net
Wed Jan 24 03:38:00 GMT 2007

Eric Blake <ebb9 <at> byu.net> writes:

> So, I retract that it is a cygwin bug, and instead claim it is an upstream 
> bug.  It is solved by doing freopen(NULL,"w",stdout) after replacing the fd, 
> since that is the mechanism for telling stdio that it should please choose 
> correct text or binary mode based on the new underlying fd.

Also vulnerable is executing a pipeline with a shell builtin when the shell's 
stdout is in text mode (I suppose a parallel bug exists with the read builtin 
on the receiving end of a pipe, but can't come up with a way to test it, since 
read is not designed to be executed in a pipeline).

$ bash -c 'echo hi | od -tx1z' | cat -A
0000000 68 69 0a                                         >hi.<$
$ bash -c 'echo hi | od -tx1z' > ~/text/x
$ cat -A ~/text/x
0000000 68 69 0d 0a                                      >hi..<$
$ bash -c '/bin/echo hi | od -tx1z' > ~/text/x
$ cat -A ~/text/x
0000000 68 69 0a                                         >hi.<$

The fix to | is separate to the fix for ``, but both instances of builtins 
using stdout to pipes will be fixed in the next bash package.

Eric Blake

Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

More information about the Cygwin mailing list