I'm curious as to why the backspace is necessary when setting IFS to split on newlines like this:
IFS=$(echo -en "\n\b")
Why can I not just use this (which doesn't work) instead?
IFS=$(echo -en "\n")
I'm on a Linux system that is saving files with Unix line endings. I've converted my file with newlines to hex and it definitely only uses "0a" as the newline character.
I've googled a lot and although many pages document the newline followed by backspace solution, none that I have found explain why the backspace is required.
-David.
Use the backslash n escape sequence to set the IFS to a newline only. Now, spaces and tabs will be ignored. Only a newline will be treated as the delimiter.
The default value of IFS is a three-character string comprising a space, tab, and newline: $ echo "$IFS" | cat -et ^I$ $ Here we used the -e and -t options of the cat command to display the special character values of the IFS variable.
Because as bash manual says regarding command substitution:
Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.
So, by adding \b
you prevent removal of \n
.
A cleaner way to do this could be to use $''
quoting, like this:
IFS=$'\n'
I just remembered the easiest way. Tested with bash on debian wheezy.
IFS=" "
no kidding :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With