3 ms·
>Those strings also support things like \0 to get a null byte WARNING: this is not true in bash! You can have exactly one null byte in a bash string: the term
by layoutIfNeeded 6y ago
>Those strings also support things like \0 to get a null byte
WARNING: this is not true in bash!
You can have exactly one null byte in a bash string: the terminating null byte. Try this:
echo $’foo\0bar’
It prints “foo”.
So practically you can’t have null bytes in bash strings, as it will be mistaken for the terminating null of the underlying C string.
In your example read -d ‘’ would work just the same; actually that’s the idiomatic way to iterate on zero-delimited input (or xargs -0). Why does the empty string work? Because -d takes the first char of the string as the delimiter, which for empty C strings is the terminating \0 - this is how bugs become features.