This
STR=\"Hello\\nWorld\"
echo $STR
produces as output
Hello\\nWorld
instead of
Hello
Wo
Those picky ones that need just the newline and despise the multiline code that breaks indentation, could do:
IFS="$(printf '\nx')"
IFS="${IFS%x}"
Bash (and likely other shells) gobble all the trailing newlines after command substitution, so you need to end the printf
string with a non-newline character and delete it afterwards. This can also easily become a oneliner.
IFS="$(printf '\nx')" IFS="${IFS%x}"
I know this is two actions instead of one, but my indentation and portability OCD is at peace now :) I originally developed this to be able to split newline-only separated output and I ended up using a modification that uses
\r
as the terminating character. That makes the newline splitting work even for the dos output ending with\r\n
.IFS="$(printf '\n\r')"