2

Although I have read Stephane's answer concentrated on basically all aspects of echo vs printf, I am unsure if it also answers my question, which stands:

The newline character produced by echo, is it always there without using any option on virtually all systems?:

I have a POSIX script, where the relevant part is:

printf '\n'; printf '\n' >&2

And I ask myself, do I need to stick with printf in this case too?

It could look better:

echo; echo >&2

But I am no expert, so I'm unsure.

Kusalananda
  • 333,661

1 Answers1

2

The newline character produced by echo, is it always there without using any option on virtually all systems?

I don't know if it's indeed always there on all systems, yet it should be there on all POSIX-derived systems. According to the specification

DESCRIPTION
The echo utility writes its arguments to standard output, followed by a <newline>. If there are no arguments, only the <newline> is written.

And later

Implementations shall not support any options.

While some implementations do support few options, in a case of no option given they should fall back to POSIX. So yes, they should produce a newline character.

In theory, if you're afraid echo doesn't comply, you may as well be afraid printf doesn't comply. In practice I would rather trust printf anyway while trying to write robust code, because it's in general the right tool and there's no point in dropping it in some edge case (see "Stepping beyond simple case" in this answer to understand what I mean).