bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: builtin read stops at '\0'


From: Greg Wooledge
Subject: Re: builtin read stops at '\0'
Date: Thu, 19 May 2011 07:59:47 -0400
User-agent: Mutt/1.4.2.3i

On Wed, May 18, 2011 at 02:10:39PM +0200, Rafaël Fourquet wrote:
>         If a line containing the '\0' is read into a variable, the content
>         of this variable does not contain the part after the '\0' character.
> 
> Repeat-By:
>         echo -e 'a\0b' | (read f; echo $f)
> 
>         The output is 'a', and '\0b' is stripped.

Bash stores strings the same way C does.  NUL marks the end of a
string.  So, even if f does contain a\0b, any attempt to retrieve
the value of f is going to stop at the NUL byte.

If you want to handle a stream of NUL-delimited strings in bash, the
best approach is to use read -d '', thus:

imadev:~$ printf '%s\0' one two three | while read -r -d '' s; do echo "<$s>"; 
done
<one>
<two>
<three>

read -d '' means "stop at NUL, instead of stopping at newline".



reply via email to

[Prev in Thread] Current Thread [Next in Thread]