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

Samuel Bronson naesten at gmail.com
Sat Jan 21 19:20:07 UTC 2012


On Sun, Jan 15, 2012 at 5:54 AM, Frank Terbeck <ft at bewatermyfriend.org> wrote:
> 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.

Yes, I understood this.

> 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'.

Hehe. You'd think they'd have formally deprecated it, rather than
settling for literal deprecation like this, though ;-).

> 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.

Yes, that's essentially what I was wondering about. (Though I was
wondering it about echo in general, rather than just zsh's.) Fair
enough.

> 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.

Yes, it does. Thanks muchly!





More information about the Pkg-zsh-devel mailing list