Append line break to command output

Solution 1:

Use echo, it will automatically append a newline:

echo "`curl -s "http://myserver.com/api?param=1&param=2"`" | sudo tee -a /var/log/myserver.log

Alternatively, you could try the -w option, but I found that it somehow prints funny characters to the console (but not to the file, luckily):

curl -s "http://myserver.com/api?param=1&param=2" -w "\n" | sudo tee -a /var/log/myserver.log

Solution 2:

Simplest is just to append a newline using echo

curl -s "http://myserver.com/api?param=1&param=2" | sudo tee -a /var/log/myserver.log && echo "" >> /var/log/myserver.log

Solution 3:

I use awk 1 for that (where 1 is just something that evaluates to true):

$ printf a|awk 1
a
$ printf a\\n|awk 1
a
$ 

It should work with gawk, BWK awk / nawk (that comes with OS X), and mawk (that comes with Debian). sed -n p works with OS X's sed but not with GNU sed.

A Bash-only alternative:

printf %s\\n "$(cat)"

Note that $() removes all linefeeds from the end, so for example echo $'a\n\n'|printf %s\\n "$(cat)" only prints one linefeed.

You could also replace printf %s\\n with echo, but for example x=-nene;echo "$x" doesn't print anything in Bash (unless xpg_echo and POSIX mode are enabled).

Solution 4:

Add the -w "\n" switch or put

-w "\n"

into your ~/.curlrc