Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a single-line command that does a curl request to check server status and then output the result to a log file. But the problem is the returned result has no line break and so the log file messes up when the command writes to the log file the next time around.

curl -s "" | sudo tee -a /var/log/myserver.log

I like to keep this to a single-line command. Any suggestion is welcome. Thank you for your time.

share|improve this question
up vote 7 down vote accepted

Use echo, it will automatically append a newline:

echo "`curl -s ""`" | 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 "" -w "\n" | sudo tee -a /var/log/myserver.log
share|improve this answer
I tried both, but they didn't work. Interestingly, the first one actually removes all line breaks from the result. Could it be due to the use of ` in the command? Thanks anyway. – GooDoo May 13 '13 at 8:39
Yeah, I assumed the output or curl would not contain line breaks. I added double quotes around the ` invocation, so that should do. The second option should work fine, though, as it only instructs curl to output a newline additionally (see man curl). – Stefan Seidel May 13 '13 at 8:48
After adding the double quotes around the ` it works! Thanks! – GooDoo May 13 '13 at 8:53

Simplest is just to append a newline with echo

curl -s "" | sudo tee -a /var/log/myserver.log && echo "" >> /var/log/myserver.log

share|improve this answer
This worked like a charm. Thanks! – GooDoo May 13 '13 at 8:36
Hmmm...when I tried this on my local machine, it works fine. But when used in the server, it says, "bash: /var/log/myserver.log: Permission denied". Adding sudo before echo didn't work either. – GooDoo May 13 '13 at 8:48
But it's not using sudo to echo into your logfile, so at least it'll need to be ... && echo | sudo tee -a ... unless sudo isn't needed? – Stefan Seidel May 13 '13 at 8:49
try (curl -s "" && echo "") | sudo tee -a ... – suspectus May 13 '13 at 8:53
@suspectus It works! Thanks! – GooDoo May 13 '13 at 9:02

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

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

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).

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .