[Pkg-zsh-devel] Bug#654444: zsh: `q' flag for paramater expansion does not work as advertised

Frank Terbeck ft at bewatermyfriend.org
Sun Jan 15 10:54:31 UTC 2012


Samuel Bronson wrote:
> Frank Terbeck wrote:
>> Samuel Bronson wrote:
[...]
>>> | naesten at hydrogen:~/hacking/crawl/crawl-ref/source% echo -n ${(q)$(echo | -e "\e")}|hd
>>> | 00000000  24 27 1b 27                                       |$'.'|
>>> | 00000004
[...]
>> zsh% printf '%s\n' ${(q)$(echo -e "\e")}
>> $'\033'
>
> Oh, really? Leaves one wondering about the point of "echo -e"...

Not really. The problem is not the inner, but the outer echo. The inner
echo ($the >$(echo -e "\e")<)produces an actual escape character (0x1b).

The (q) produces an octal escape in $'...' quotes (0x1b == 033o). The
outer echo sees the octal escape and interprets it, producing a literal
ASCII escape character again.

Echo is like that - or not, depending on the shell in question. Even
POSIX says:

[SUSv3, echo]
  New applications are encouraged to use printf instead of echo.
[...]
  The echo utility has not been made obsolescent because of its
  extremely widespread use in historical applications. Conforming
  applications that wish to do prompting without <newline>s or that
  could possibly be expecting to echo a -n, should use the printf
  utility derived from the Ninth Edition system.
[/SUSv3]

In other words: `echo' is crap. We've just not removed it, because it's
in heavy use. If you want consistent predictable behaviour, use
`printf'.

If you're wondering why zsh's `echo' has the `-e' option at all,
although it interprets the escape sequences anyway, then the answer is:
Because zsh tries to please everyone. There is an `-E' option, that
disables the escape sequences effect. And there's a `BSD_ECHO' option,
that makes that behaviour the default for zsh's `echo'. In order to get
the interpreting behaviour with that option set, the `-e' option is
provided.

This is yet another reason not to use echo. Different shells have
annoyingly different echos and in zsh you can even alter its behaviour
by options.

I hope that clears things up.

Regards, Frank





More information about the Pkg-zsh-devel mailing list