
How to handle the most difficult file names Observe that with -d$'\t', read divides its input into "lines" based on tab characters. Here, the $'.' construct was used to enter special characters like newline, \n and tab, \t. $ echo $'line one \n line\t two \n line three\t ends here' | while IFS= read -r -d$'\t' line do echo "=$line=" done For example, we can terminate lines with a tab character: $ echo $'line one \n line\t two \n line three\t ends here' d changes the delimiter character that read uses to mark the end of an input line.

The behavior with multiple line input, however, can be changed drastically using read's -d flag. r behaves similarly with the exception that, without -r, multiple lines can be combined into one line using the trailing backslash as shown above. Since read takes input one line at a time, IFS behaves affects each line of multiple line input in the same way that it affects single line input. If you want backslashes in the input to be taken as plain characters, then use -r. In sum, if you want backslashes in the input to have special meaning, don't use -r. Second, the backslash on the end of the first line was interpreted as a line-continuation character and the two lines were merged into one.

First, the double-backslash was converted to a single backslash. Observe what happens with -r: $ | while IFS= read line do echo "=$line=" done To illustrate, we use two echo commands that supply two lines to the while loop. The -r option prevents read from treating backslash as a special character. But, the leading and trailing white space have been removed. In this version, the white space internal to the line is still preserved.

Now, consider the same statement with the default IFS: $ echo " this is a test " | while read -r line do echo "=$line=" done The line variable contains all the white space it received on its stdin. To illustrate, first observe with IFS set to nothing: $ echo " this is a test " | while IFS= read -r line do echo "=$line=" done The effect in that loop is to preserve leading and trailing white space in line. IFS does many things but you are asking about that particular loop.
