The following few threads on this site and StackOverflow were helpful for understanding how IFS works:
- What is IFS in context of for looping?
- How to loop over the lines of a file
- Bash, read line by line from file, with IFS
But I still have some short questions. I decided to ask them in the same post since I think it may help better future readers:
Q1. IFS is typically discussed in the context of "field splitting". Is field splitting the same as word splitting ?
Q2: The POSIX specification says:
If the value of IFS is null, no field splitting shall be performed.
Is setting IFS= the same as setting IFS to null? Is this what is meant by setting it to an empty string too?
Q3: In the POSIX specification, I read the following:
If IFS is not set, the shell shall behave as if the value of IFS is
<space>, <tab> and <newline>
Say I want to restore the default value of IFS. How do I do that? (more specifically, how do I refer to <tab> and <newline>?)
Q4: Finally, how would this code:
while IFS= read -r line
do
echo $line
done < /path_to_text_file
behave if we we change the first line to
while read -r line # Use the default IFS value
or to:
while IFS=' ' read -r line