zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Aryn Starr <whereislelouch@icloud.com>
Cc: zsh-workers@zsh.org
Subject: Re: Possible :q quoting bug
Date: Thu, 8 Aug 2019 22:48:18 +0100	[thread overview]
Message-ID: <20190808214818.fmfzcyqvzjtrmkpr@chaz.gmail.com> (raw)
In-Reply-To: <5E224043-AA93-4B39-94BB-6230A9A2737F@icloud.com>

2019-08-08 20:36:00 +0430, Aryn Starr:
> I expect this function to be idempotent on the command line:
> ```
> Function reveal() {
> eval “${@:q}”
> }
> ```
> But it’s not. It loses empty args.
> I have written the following function to fix this ‘bug’:
> ```
> gquote () {
> 	local i
> 	for i in "$@"
> 	do
> 		test -z "$i" && print -rn "''" " " || print -rn -- "${i:q}" " "
> 	done
> }
> ```

You may want to use:

reveal() eval "${(qq)@}"

which is the zsh way to do it.

$var:q is reminiscent of tcsh's $var:q though works differently
(in tcsh, you do need cmd $var:q to pass the content of $var
literally to cmd as tcsh has a completly fucked-up way of
handling parameter expansions (hardly improved from the Thompson
shell's simplistic parameter expansion from the early 70s).

But like zsh's, tcsh's $var:q expands to nothing when $var is
empty or a list of empty elements.

Of the various ${(q)var}, ${(qq)var}, ${(qqq)var}, ${(qqqq)var}
${(q+)var}..., ${(qq)var} is the only one that is
localisation-safe as it always using single quotes. The other
ones (and also the quoting operators of other shells or of
various printf %q) are generally not safe as they may quote some
characters with \ (or use things like $'\n') and the encoding of
\ is found in the encoding of some multi-byte characters in some
locales.

Note that mksh added a ${var@Q} which was later copied by bash.

Initially, ${empty@Q} expanded to nothing (and it still does in
bash), but that changed at some point in mksh where it now
expands to ''. (possibly bash will follow).

I'd agree zsh's documentation is misleading though as it
currently says:

q
     Quote the substituted words, escaping further substitutions.  Works
     with history expansion and parameter expansion, though for
     parameters it is only useful if the resulting text is to be
     re-evaluated such as by eval.


-- 
Stephane

  parent reply	other threads:[~2019-08-08 21:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-08 16:06 Aryn Starr
2019-08-08 21:37 ` Bart Schaefer
2019-08-08 21:48 ` Stephane Chazelas [this message]
2019-08-09 11:00   ` Aryn Starr

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=20190808214818.fmfzcyqvzjtrmkpr@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=whereislelouch@icloud.com \
    --cc=zsh-workers@zsh.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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