I want to iterate through a list of files without caring about what characters the filenames might contain, so I use a list delimited by null characters. The code will explain things better.
# Set IFS to the null character to hopefully change the for..in
# delimiter from the space character (sadly does not appear to work).
IFS=$'\0'
# Get null delimited list of files
filelist="`find /some/path -type f -print0`"
# Iterate through list of files
for file in $filelist ; do
# Arbitrary operations on $file here
done
The following code works when reading from a file, but I need to read from a variable containing text.
while read -d $'\0' line ; do
# Code here
done < /path/to/inputfile
bash: warning: command substitution: ignored null byte in input. This is because bash is intended for posix derivative environments, in which env vars are internally stored in a null-terminated buffer, and bash vars are (in every case I've ever examined) host env vars.test=$'a\011b';echo ${#test} ="${test}"=results in3 =a b=. Then try an octal 0 :test=$'a\0b';echo ${#test} ="${test}"=results in1 =a=; this reports the zero-terminated string length of $test as 1, but the 'b' and another zero may still be stored into the variable, we do not know.