Shell – Handling NULL characters in shell

binaryshell

Is there a portable way of handling NULL characters in shell?

A typical example would be splitting the output of find ... -print0 with shell (and shell only) either in a pipe or in a command substitution result. By portable I mean ideally something that shells not as powerful as e.g. bash or zsh wouldn't choke on. Is this possible in a "bare POSIX shell" (any POSIX version)?

Best Answer

POSIX doesn't envision the standard utilities to deal with text embedding null characters. The -print0 option you use with find is itself a GNU extension unsupported by POSIX.

One way to deal with a flow of data containing nulls with POSIX shell scripting would be to convert it first to real text with od and process that text instead.

In any case, if you have GNU find, you likely have other GNU utilities that haven't that limitation in the first place.