Re: The read builtin erroneously consumes null bytes

Harald van Dijk <[email protected]> Sat, 20 Jun 2026 03:51:47 +0100
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
On 6/20/26 02:52, Alexis wrote:
> "Kerin Millar" <[email protected]> writes:
> 
>> Consider the following test case. It demonstrates NUL being consumed 
>> by the read builtin, rather than by cat(1) as it should have been.
>>
>> $ printf 'a\n\0bc' | dash -c 'read x; cat' | od -An -t x1 -c
>>   62  63
>>    b   c
> 
> POSIX doesn't require a `read` _builtin_,

It is not possible to implement the read utility as an external utility 
because the read utility is required to modify shell variables, so 
effectively it is pretty much required to be a builtin.

>                                           but for the `read` _utility_, 
> it says:
> 
>> STDIN
>>
>> If the -d delim option is not specified, or if it is specified and
>> delim is not the null string, the standard input shall contain zero or
>> more bytes (which need not form valid characters) and shall not
>> contain any null bytes.

The input to 'read' does not contain any null bytes. The input to 'read' 
is just 'a' followed by a newline and 'read' is supposed to stop reading 
after that.

Cheers,
Harald van Dijk