dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Kevin Korb <kmk@sanitarium.net>
Cc: Eric Blake <eblake@redhat.com>, dash@vger.kernel.org
Subject: Re: [bug?] echo -n does not work as described
Date: Thu, 12 Nov 2015 21:24:45 +0000	[thread overview]
Message-ID: <20151112212445.GA12431@chaz.gmail.com> (raw)
In-Reply-To: <56440B7A.4040508@sanitarium.net>

2015-11-11 22:46:02 -0500, Kevin Korb:
[...]
> >>> NOTE: your shell may have its own version of echo, which
> >>> usually super‐ sedes the version described here. Please refer
> >>> to your shell's docu‐ mentation for details about the options
> >>> it supports.
> > 
> >> By the way, 'echo -n' is non-portable, and POSIX says you should 
> >> use 'printf' instead.
> 
> If dash refuses to supply a compatible version of echo then dash must
> not include a builtin version of echo.  If dash didn't have a broken
> embedded version of echo I wouldn't have a problem.
[...]

To clarify, a few things, sh and the POSIX/Unix utilities like C
nowadays is a language that has a (well several) standard that
defines it, and several existing implementations of interpreters
that interpret that language.

Among the most recognised specifications, we have POSIX and
Unix. Both specifications have now been merged. To simplify, the
Unix spec is like the POSIX spec but where a few optional parts
are required.

So basically we have the POSIX syntax for sh and utilities and
the Unix syntax.

In the POSIX syntax, the behaviour for:

echo -n whatever

is unspecified. That means that a POSIX conformant script, or a
script written to be portable to POSIX conformant systems must
not call echo with a first argument that may be -n. We're told
to use printf instead there:

printf %s "$var"
instead of
echo -n "$var"

Same goes for any argument that contains backslash character.

In the Unix syntax however, the behaviour of echo is clearly
specified.

echo -n whatever

is required to output "-n whatever<newline>", so your dash is
both POSIX and Unix conformant in that regard.

The builtin echo of bash, by default is neither POSIX nor Unix
conformant unless both the xpg_echo and posix (that one enabled
when called as sh) options have been enabled.

More details at:

http://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo

In short, don't use echo.

-- 
Stephane

  parent reply	other threads:[~2015-11-12 21:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-12  1:56 [bug?] echo -n does not work as described Kevin Korb
2015-11-12  3:14 ` Eric Blake
2015-11-12  3:40   ` Kevin Korb
2015-11-12  3:46     ` Kevin Korb
2015-11-12  8:35       ` Seb
2015-11-12 16:12         ` Kevin Korb
2015-11-12 21:24       ` Stephane Chazelas [this message]
2015-11-12  8:10 ` Bastian Bittorf
2015-11-12 16:11   ` Kevin Korb
2015-11-19 12:36     ` Bastian Bittorf
2015-11-12 10:03 ` Petr Šabata
2015-11-12 16:18   ` Kevin Korb
2015-11-12 16:55     ` Kevin Korb

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151112212445.GA12431@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=dash@vger.kernel.org \
    --cc=eblake@redhat.com \
    --cc=kmk@sanitarium.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).