Bash Version: GNU bash, version 4.1.7(1)-release (amd64-portbld-freebsd8.0)
          OS: FreeBSD 8.0
    Hardware: amd64
 Environment: jail
 Description: read terminates reading all records at first null-byte ( chr(0) ) 
in a stream, null-bytes are valid ascii characters and should not cause read to 
stop reading
              a line this behavior is not reproducible using bourne shell.
        
Steps To Reproduce:

[bash ~]$ printf 'foo\0bar\n' | while read line; do echo "$line"; done
foo
[bash ~]$ # verify that printf is yielding the expected output
[bash ~]$ printf 'foo\0bar\n' | od -a
0000000    f   o   o nul   b   a   r  nl                                
0000010
[bash ~]$ # verify that it is not just echo with awk
[bash ~]$ printf 'foo\0bar\n' | while read line; do awk -v line="$line" 'BEGIN 
{ print line; }'; done | od -a
0000000    f   o   o  nl                                                
0000004
[bash ~]$ # same awk test with a subshell and no read -- note that null-byte is 
removed, but feed does not end
[bash ~]$ awk -v line=`printf 'foo\0bar\n'` 'BEGIN { print line; }' | od -a
0000000    f   o   o   b   a   r  nl                                    
0000007'
[bash ~]$ # behavior with multiple lines
[bash ~]$ printf 'foo\0bar\nbaz\n' | while read line; do echo "$line"; done
foo
baz
[bash ~]$ # behavior with read -r
[bash ~]$ printf 'foo\0bar\nbaz\n' | while read -r line; do echo "$line"; done
foo
baz

Behavior in bourne shell:

$ # note that the null-byte is dropped, but the line is read
$ printf 'foo\0bar\nbaz\n' | while read line; do echo "$line"; done | od -a
0000000    f   o   o   b   a   r  nl   b   a   z  nl                    
0000013
$ # test with awk instead of echo
$ printf 'foo\0bar\nbaz\n' | while read line; do awk -v line="$line" 'BEGIN { 
print line; }'; done | od -a
0000000    f   o   o   b   a   r  nl   b   a   z  nl                    
0000013

Reply via email to