If Ifs And Buts Were Candy And Nuts

At the beginning of a bash shell script is the following line: IFS=$'\n' What is the meaning behind this collection of symbols?

If Ifs And Buts Were Candy And Nuts 1

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...

I was reading this Q&A: How to loop over the lines of a file? What is the IFS variable? And what is its usage in the context of for-loops?

If Ifs And Buts Were Candy And Nuts 3

@shadowtalker, though you do sometimes see IFS=$'\n' read -r line being used here and there, it is pointless as read -r line reads the input up to a newline, but the newline is not included in what is read, so read could never split on newline on the result. So it's exactly the same as IFS= read -r line. In any case, it's better than forgetting the IFS=. It could make sense if using a ...

If Ifs And Buts Were Candy And Nuts 4

We change IFS and may forget to restore it We restore IFS every single iteration of the loop I discovered that while IFS=... can be used here like that: ... But this is not an option because read -p prompt gets corrupted by continuous input stream A solution would be IFS set only for one for statement like this: ... but bash disallows that.

If Ifs And Buts Were Candy And Nuts 5

For loop over lines -- how to set IFS only for one for statement?

If only there was a variable whose value is a space… Or more generally, contains a space. cat${IFS}file.txt The default value of IFS is space, tab, newline. All of these characters are whitespace. If you need a single space, you can use ${IFS%??}. More precisely, the reason this works has to do with how word splitting works. Critically, it's applied after substituting the value of variables ...

If Ifs And Buts Were Candy And Nuts 7