Skip to contentSkip to conversation

echo do not print NUL byte

DISCUSSION Trek 4 participants 5 messages Jan 20, 2016 – Jan 21, 2016

lore
Share & tools
lore

Trek

author
#0 lore raw

there is no way to print the NUL byte with the echo utility

with an XSI shell (mksh, posh, yash):

  $ sh -c 'echo \\0 | od -An -c'
    \0  \n

with a shell that requires echo -e (ksh, bash, busybox, mksh):

  $ sh -c 'echo -e \\0 | od -An -c'
    \0  \n

with dash (XSI, no -e argument):

  $ dash -c 'echo \\0 | od -An -c'
    \n

where others characters are correctly printed:

  $ dash -c 'echo \\0100 | od -An -c'
     @  \n

with dash it only works with printf:

  $ dash -c 'printf \\0\\n | od -An -c'
    \0  \n

I discovered this bug developing a small shell library, where you can find an use case in the n_bytes_encode function of the lib/bytes.sh file included in http://www.trek.eu.org/devel/naive/naive-0.0.2.tar.gz

c-ya!

#1 lore raw
Quoted 1 line · Trek

there is no way to print the NUL byte with the echo utility

POSIX says you should use 'printf', not 'echo', if you expect to use any \ in the arguments. That said, the man page says:

     echo [-n] args...

...
\0digits
Output the character whose value is given by zero to
three
octal digits. If there are zero digits, a nul character is output.

Quoted 4 lines

with dash (XSI, no -e argument):

  $ dash -c 'echo \\0 | od -An -c'
    \n

So this is behavior in disagreement with the documentation, so you definitely have discovered a bug. Of course, since POSIX already says the use of \ is implementation-defined if XSI is not in force, and dash doesn't necessarily strive for full XSI compliance, it might be easier to patch the docs than to output a NUL byte.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
In reply to Trek

Hi,

Quoted 6 lines · Trek

there is no way to print the NUL byte with the echo utility

with dash (XSI, no -e argument):

   $ dash -c 'echo \\0 | od -An -c'
     \n

This is https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=379227, which did get fixed already, but it was after 0.5.8. That should mean it will be in the next release of dash, but you can check out the sources from Git if you want to see it now.

Cheers,
Harald van Dijk