read nul delimited fields

依然范特西╮ 提交于 2019-12-05 18:25:12

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.

As a workaround, I created this function

function read_loop {
  while [ "$#" -gt 0 ]
  do
    read -d '' "$1"
    shift
  done
}

Example use

read-nul mike november oscar < delta.txt
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!