When setting IFS to split on newlines, why is it necessary to include a backspace?
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 :)
It's a hack because of the use of echo
and command substitution.
prompt> x=$(echo -en "\n")
prompt> echo ${#x}
0
prompt> x=$(echo -en "\n\b")
prompt> echo ${#x}
2
The $()
strips trailing newlines and \b
prevents \n
from being a trailing newline while being highly unlikely to appear in any text. IFS=$'\n'
is the better way to set IFS to split on newlines.