When setting IFS to split on newlines, why is it necessary to include a backspace?

后端 未结 5 1201
灰色年华
灰色年华 2020-12-12 20:29

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

5条回答
  •  余生分开走
    2020-12-12 20:46

    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'
    

提交回复
热议问题