Suppress echo newlines


In my continuing series of things you already know, unless you don’t, today I learned of an alternative to printing strings in the shell, sans newline.

I was reading the FreeBSD sh(1) manpage, like a gentleman:

 echo [-e | -n] [string ...]
   Print a space-separated list of the arguments to the
   standard output and append a newline character.
   -n  Suppress the output of the trailing newline.

Sure enough:

$ echo saywhat
==> saywhat
==> $

And with -n:

$ echo -n saywhat
==> saywhat$

This whole time I’d been doing this to avoid a newline:

$ printf '%s' saywhat

Good ol’ Kenneth Almquist, and the FreeBSD maintainers :).

But does this also apply to csh, the shell and scripting language allegedly considered harmful, and therefore you should write in? Under echo_style in the tcsh(1) manpage:

bsd  Don't echo a newline if the first argument is `-n`;
     the default for csh.


Author bio and support


Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.