Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 some command which produces output with no new line at the end, like this

Myprompt$ somecmd

Currently I overcome this by somecmd | sed 's/$/\n/' | tr -s '\n' Is there a better way to do this?

share|improve this question
up vote 14 down vote accepted

Just run echo after it, it should generate a newline

Myprompt$ somecmd ; echo

And If you need to feed it to something else, run it in a sub-shell:

Myprompt$ ( somecmd ; echo ) | someothercmd

Or.. as @camh points out, the subshell is actually not needed you can execute it with a command list in the current shell environment with:

Myprompt$ { somecmd ; echo ; } | someothercmd
share|improve this answer
A subshell is not needed. You can just do { somecmd ; echo ; } | someothercmd – camh Jan 13 '11 at 12:50
Then I'll shamelessly incorporate your better solution into my answer :) – Kjetil Jorgensen Jan 13 '11 at 12:56
Thanks. Can you explain what this does { somecmd ; echo ; } . I understand how the subshell worked. Where should I read about this? – balki Jan 14 '11 at 7:34
If you're wondering about the { list; } form, see the "Compound Commands" section of the manual page for bash. It executes a command-list in the current shell environment. The reason we use echo at the end is just to append a newline, by default echo emits a trailing newline. The reason we use the { list; } form has to do with the way bash parses and sets up redirections/pipes. somecmd; echo | someothercmd, will just connect a pipe between echo and someothercmd. With the compound command, the entire command list will have it's stdout connected with a pipe to someothercmd. – Kjetil Jorgensen Jan 14 '11 at 10:43

Feed it through some utility which read input in lines and output lines, like in awk { print $0 }.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.