Read fields separated by zero

Given this file

printf 'alpha\0bravo\0charlie' > delta.txt

      

I would like to read the fields in separate variables. The reason I use the null separator is because the fields will contain paths to files that can contain any character other than null. I've tried these commands:

IFS= read mike november oscar < delta.txt
IFS=$'\0' read mike november oscar < delta.txt

      

However, the fields will not split correctly

$ echo $mike
alphabravocharlie

      

+3


source to share


2 answers


As a workaround, I created this function

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

      



Usage example

read-nul mike november oscar < delta.txt

      

+1


source


The assignment IFS=$'\0'

does not make the null character a delimiter, as Bash variables cannot contain null characters . IFS=$'\0'

is equivalent IFS=

, which you can check by doing:

bash-4.3$ IFS=$'\0'
bash-4.3$ echo ${#IFS}
0

      

And IFS=

by definition means that the word doesn't break at all (see the Bash Reference Manual ).

What you can do is read the null-delimited elements one at a time using the -d

read

builtin
option . According to the linked documentation,

-d delim

The first character is delim

used to terminate an input line, not a new line.



We can use an empty string delim

to get the desired behavior.

Example (I took the liberty of adding a space to your example to demonstrate how it achieves what I want - no spaces are separated):

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 also use an option -r

to keep backslashes in the input file. Of course, you can replace < delta.txt

with cat delta.txt |

at the beginning.

I know reading one by one is annoying, but I can't think of anything better.

+2


source







All Articles