Suppress echo newlines

Software

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.

Boom!

Author bio and support

Me!

Ruben Schade is a technical writer and IaaS engineer in Sydney, Australia who refers to himself in the third person in bios. Wait, not BIOS… my brain should be EFI by now.

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

If you found this post helpful or entertaining, you can shout me a coffee or buy some silly merch. Thanks!