How can I print a newline as \n in Bash?

bash, shell

Solution

Here's my solution:

sed 's/$/\\n/' | tr -d '\n'

Problem

Basically, I want to achieve something like the inverse of `echo -e`. I have a variable which stores a command output, but I want to print newlines as \n.

Original source