Subject: Re: bug#32703: echo_man_error

From: Eric Blake <[email-masked]>
Date: Tue, 11 Sep 2018 11:01:59 -0500

tag 32703 needinfo
thanks

On 9/11/18 6:32 AM, 1064240043 wrote:
> In Manual page echo(1)
> 
> 
> it says '-E disable interpretation of backslash escapes (default)'
> 
> 
> but I tried and found out that enable(-e) is default.

What exactly did you try? And are you sure you are testing the same echo 
in your various tests?  Remember that many shells ship echo as a 
built-in (both bash and dash, which are the two most common choices for 
/bin/sh on a GNU/Linux installation), so you have to first determine 
whether you are testing the behavior of your shell instead of the echo 
built as part of coreutils (which is often installed at /bin/echo).

By the way, POSIX requires echo to behave with backslash escapes enabled 
by default (dash does this, and bash can be convinced to do it via 'set 
-o posix && shopt -s xpg_echo').  But oddly enough, coreutils does NOT 
quite seem to obey POSIX, even under POSIXLY_CORRECT.  Note:

$ /bin/echo --version | head -n1
echo (GNU coreutils) 8.29
$ POSIXLY_CORRECT=1 /bin/echo --version
--version

(so far, so good - printing --help/--version is a useful GNU extension, 
but violates POSIX, so we turn it off under POSIXLY_CORRECT)

$ bash -c 'echo a\\nb'
a\nb
$ bash -c 'set -o posix; shopt -s xpg_echo; echo a\\nb'
a
b
$ dash -c 'echo a\\nb'
a
b

(okay, we've demonstrated that bash defaults to non-POSIX behavior, but 
that it can be coerced into obeying; and dash defaults to POSIX behavior)

$ /bin/echo a\\nb
a\nb

(so far, so good - we've chosen to NOT expand backslash by default, 
which matches bash's defaults even if it violates POSIX)

$ POSIXLY_CORRECT=1 /bin/echo a\\nb
a\nb

Yikes!  Even though we asked for POSIX correctness, we are NOT 
interpreting backslashes.  I think this is a bug in GNU coreutils' echo, 
and could be fixed by the patch below (but the testsuite would also need 
updates).

But also note that POSIX has some rather wonky wording - it permits -n 
to have implementation-defined behavior, but forgot to give special 
exceptions to -e or -E.  In short, the POSIX recommendation is that you 
NEVER use 'echo' if you are trying to print a string that might start 
with '-' or contain '\' - use printf instead.

diff --git i/src/echo.c w/src/echo.c
index 9958a8ec6..b3dfdb54a 100644
--- i/src/echo.c
+++ w/src/echo.c
@@ -189,7 +189,7 @@ main (int argc, char **argv)

  just_echo:

-  if (do_v9)
+  if (do_v9 || getenv ("POSIXLY_CORRECT"))
      {
        while (argc > 0)
          {

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org