The assignment IFS=$'\0'
doesn't make the null character a delimiter, since Bash variables cannot hold null characters. IFS=$'\0'
is equivalent to IFS=
, which you can verify by doing:
bash-4.3$ IFS=$'\0'
bash-4.3$ echo ${#IFS}
0
And IFS=
by definition means no word splitting at all (see Bash Reference Manual).
What you can do is read the null delimited items one by one using the -d
option of the read
builtin. According to linked documentation,
-d delim
The first character of delim
is used to terminate the input line, rather than newline.
We can use a null string for delim
to get desired behavior.
Example (I took the liberty to add whitespace to your example to demonstrate how it achieves what to want — no splitting on whitespace):
bash-4.3$ printf 'alpha with whitespace\0bravo with whitespace\0charlie with whitespace' > delta.txt
bash-4.3$ { read -r -d '' mike; IFS= read -r -d '' november; IFS= read -r -d '' oscar; echo $mike; echo $november; echo $oscar; } < delta.txt
alpha with whitespace
bravo with whitespace
charlie with whitespace
I'm also using the -r
option to preserve backslashes in the input file. Of course you can replace < delta.txt
with a cat delta.txt |
at the beginning.
I know reading one by one is annoying, but I can't think of anything better.