IFS, the Internal Field Separator
The shell uses this to determine how to do
word splitting i.e. separate fields.
In this example, it is changed to commas.
It may be used to parse CSV, TSV,
and generally semi-structured mark-up
with a simple field separation scheme.
mystring="one two,three;four"
IFS=','
for word in $mystring; do echo $word; done
one two
three;four
Looping on files or lines, using IFS, with while loops.
Snippet making a for loop iterate over the output of find.
This will trip shellcheck, warning that using for is fragile over the output of find.
The reason is that if the current IFS is included in a filename, the loop will
treat it as two separate items, causing unintended behaviours.
for file in "$(find .)"; do
echo "$file"
done
Another syntax, less readable, but more inline
find . -print0 | while IFS= read -rd '' file; do \
echo "$file"; done
By using the flag -print0,
making find separate the filenames
with a null character (a zero byte).
Note: Piping find -print0
into xargs calls for using
xargs flag -0, standard operation.
read reads stdin one byte at a time,
until it finds an unescaped newline character,
or end-of-input.
When doing IFS= read -r line,
IFS is first set to an empty string.
Therefore no splitting will occur.
The special syntax VAR=value command args,
makes the IFS change only affect that statement,
any following commands isn't affected.
Therefore read reads the whole line,
assigning it to the first variable $line.