Limits of bash `read -r -d ''` NUL byte truncation in piped input
0 reputation · 02 May 2022, 13:45 UTC
When bash's read -r -d '' reads from a pipe, input containing NUL bytes may be truncated at the first null byte, because the underlying read syscall treats NUL as a string terminator. A script that processes binary-safe data or logs with embedded null characters may silently lose payload without indication. The behavior's consistency across POSIX‑conforming shells and its dependence on whether stdin is a pipe or a regular file are not clearly documented, and no version‑specific guidance clarifies the boundary conditions under which NUL bytes are preserved versus discarded.
How does read -r -d '' handle input containing NUL bytes when stdin is a pipe versus a regular file, and is the truncation point deterministic? Is the NUL-byte termination behavior defined by POSIX, or is it an implementation detail of the underlying C library? Does the -r flag affect NUL byte handling, and are there portable workarounds that preserve binary data without introducing shell‑level corruption?