The \b
char as a suffix of newline \n
is added due to removal of the tailing \n
in the command substitution $(...)
. So the \b
is used as a suffix to \n
, and by that \n
is no longer trailing, so it is returned from the the command substitution.
The side effect is, that IFS
also will include the \b
char as a separator as well, instead of just \n
, which really is our sole interest.
If you expect \b
may someday appear in the string (why not?), then you may use:
IFS="$(printf '\nx')" && IFS="${IFS%x}";
that returns \n suffixed with x
&&
removes x
now IFS contains only the \n
char.
IFS="$(printf '\nx')" && IFS="${IFS%x}";
echo ${#IFS}; # 1
and nothing will break in case of \b
, test:
#!/bin/sh
sentence=$(printf "Foo\nBar\tBaz Maz\bTaz");
IFS="$(printf '\nx')" && IFS="${IFS%x}";
for entry in $sentence
do
printf "Entry: ${entry}.\n";
done
gives two lines (due to one \n
):
Entry: Foo.
Entry: Bar Baz Maz Taz.
as expected.
IFS="$(printf '\nx')" && IFS="${IFS%x}";
using:
IFS="
"
gives the same result, but these two lines must not be indented, and if you accidentally put space or tab or any other white char between " and ", you'll no longer have only the \n
char there, but some "bonuses" as well.
This bug will be hard to spot, unless you use the option "show all characters" in your editor.