zsh-workers
 help / Atom feed
* Possible :q quoting bug
@ 2019-08-08 16:06 Aryn Starr
  2019-08-08 21:37 ` Bart Schaefer
  2019-08-08 21:48 ` Stephane Chazelas
  0 siblings, 2 replies; 4+ messages in thread
From: Aryn Starr @ 2019-08-08 16:06 UTC (permalink / raw)
  To: zsh-workers

Hi,
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
}
```

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Possible :q quoting bug
  2019-08-08 16:06 Possible :q quoting bug Aryn Starr
@ 2019-08-08 21:37 ` Bart Schaefer
  2019-08-08 21:48 ` Stephane Chazelas
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2019-08-08 21:37 UTC (permalink / raw)
  To: Aryn Starr; +Cc: zsh-workers

On Thu, Aug 8, 2019 at 1:18 PM Aryn Starr <whereislelouch@icloud.com> wrote:
>
> Hi,
> I expect this function to be idempotent on the command line:
> ```
> Function reveal() {
> eval “${@:q}”
> }
> ```
> But it’s not. It loses empty args.

You want ${(q)@} rather than ${@:q}.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Possible :q quoting bug
  2019-08-08 16:06 Possible :q quoting bug Aryn Starr
  2019-08-08 21:37 ` Bart Schaefer
@ 2019-08-08 21:48 ` Stephane Chazelas
  2019-08-09 11:00   ` Aryn Starr
  1 sibling, 1 reply; 4+ messages in thread
From: Stephane Chazelas @ 2019-08-08 21:48 UTC (permalink / raw)
  To: Aryn Starr; +Cc: zsh-workers

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Possible :q quoting bug
  2019-08-08 21:48 ` Stephane Chazelas
@ 2019-08-09 11:00   ` Aryn Starr
  0 siblings, 0 replies; 4+ messages in thread
From: Aryn Starr @ 2019-08-09 11:00 UTC (permalink / raw)
  To: Stephane Chazelas; +Cc: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 2533 bytes --]

Thanks! I suggest adding this little tidbit to the manpage.
I tested `”${(q@)@}” “${(q)@}” “${(qq@)@}”`. The only one to actually be idempotent (on my English locale at least) is `”${(q@)@}”. If you use `”${(qq@)@}”, you’ll lose aliases, which can be dangerous, when, e.g., you have aliases `rm` to `rm -i`. (Though generally I use aliases as little functions.)

> On Aug 9, 2019, at 2:18 AM, Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> 
> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-08 16:06 Possible :q quoting bug Aryn Starr
2019-08-08 21:37 ` Bart Schaefer
2019-08-08 21:48 ` Stephane Chazelas
2019-08-09 11:00   ` Aryn Starr

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/ public-inbox