zsh-users
 help / color / mirror / code / Atom feed
* preserving single quotes
@ 2022-09-28  3:12 Ray Andrews
  2022-09-28  9:10 ` Roman Perepelitsa
  2022-09-28 10:00 ` Stephane Chazelas
  0 siblings, 2 replies; 6+ messages in thread
From: Ray Andrews @ 2022-09-28  3:12 UTC (permalink / raw)
  To: Zsh Users

I've done this before but I can't remember the invocation.

    $ dd="echo howdy"

    $ ee=$($=dd); echo $ee
    howdy

    $ dd="aptitude search '?name(libreoffice-java-common)'"

    $ ee=$($=dd); echo $ee

    $ (no output)


... I have to be able to run the aptitude command with the single quotes 
intact.  I've tried every little trick that's worked before but I'm just 
not finding the magic.



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

* Re: preserving single quotes
  2022-09-28  3:12 preserving single quotes Ray Andrews
@ 2022-09-28  9:10 ` Roman Perepelitsa
  2022-09-28 10:00 ` Stephane Chazelas
  1 sibling, 0 replies; 6+ messages in thread
From: Roman Perepelitsa @ 2022-09-28  9:10 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Wed, Sep 28, 2022 at 5:13 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> I've done this before but I can't remember the invocation.
>
>     $ dd="echo howdy"
>
>     $ ee=$($=dd); echo $ee
>     howdy
>
>     $ dd="aptitude search '?name(libreoffice-java-common)'"
>
>     $ ee=$($=dd); echo $ee

Use ${(Q)${(z)dd}} instead of $=dd. See `man zshexpn` for the meaning
of (z) and (Q).

Roman.


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

* Re: preserving single quotes
  2022-09-28  3:12 preserving single quotes Ray Andrews
  2022-09-28  9:10 ` Roman Perepelitsa
@ 2022-09-28 10:00 ` Stephane Chazelas
  2022-09-28 14:32   ` Ray Andrews
  1 sibling, 1 reply; 6+ messages in thread
From: Stephane Chazelas @ 2022-09-28 10:00 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

2022-09-27 20:12:22 -0700, Ray Andrews:
> I've done this before but I can't remember the invocation.
> 
>    $ dd="echo howdy"
> 
>    $ ee=$($=dd); echo $ee
>    howdy
> 
>    $ dd="aptitude search '?name(libreoffice-java-common)'"
> 
>    $ ee=$($=dd); echo $ee
> 
>    $ (no output)
> 
> 
> ... I have to be able to run the aptitude command with the single quotes
> intact.  I've tried every little trick that's worked before but I'm just not
> finding the magic.


Use either:

dd() aptitude search '?name(libreoffice-java-common)'
ee=$(dd)

Or:

dd="aptitude search '?name(libreoffice-java-common)'"
ee=$(eval -- $dd)

Or:

dd=( aptitude search '?name(libreoffice-java-common)' )
# or
dd=(
  aptitude
  search
  '?name(libreoffice-java-common)'
)
# just to show that it's several arguments you want to store in
# that array
ee=$( "$dd[@]" )

$=dd just does $IFS-splitting. If $dd is meant to contain shell code,
you should use eval to evaluate it. But to store code, you
generally use functions not variables. The z and Q parameter
expansion flags can do the same tokenisation and quote removal
as the shell syntax parser does, but I don't think you want to
go there.

-- 
Stephane


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

* Re: preserving single quotes
  2022-09-28 10:00 ` Stephane Chazelas
@ 2022-09-28 14:32   ` Ray Andrews
  2022-09-28 18:49     ` Stephane Chazelas
  0 siblings, 1 reply; 6+ messages in thread
From: Ray Andrews @ 2022-09-28 14:32 UTC (permalink / raw)
  To: zsh-users

On 2022-09-28 03:00, Stephane Chazelas wrote:

> Use ${(Q)${(z)dd}} instead of $=dd. See `man zshexpn` for the meaning
> of (z) and (Q).
>
> Roman.
Didn't work.
>
> Use either:
>
> dd() aptitude search '?name(libreoffice-java-common)'
> ee=$(dd)

Cool.  So we can just create a little impromptu function.  Never tried 
that but it works.  I sorta understand that since the string isn't being 
handed off to something else, zsh doesn't feel the need to strip off the 
quotes -- the function is just as written.
>
> Or:
>
> dd="aptitude search '?name(libreoffice-java-common)'"
> ee=$(eval -- $dd)
My old friend eval.  But Bart advises to get away from it.  Anyway it 
works for now.

> Or:
>
> dd=( aptitude search '?name(libreoffice-java-common)' )
> # or
...
> ee=$( "$dd[@]" )
Works.  But that puzzles me, it seems simplest, but how is that that 
saving the aptitude string as an array preserves the single quotes? It 
seems unintuitive that the method of saving the string would make the 
difference.

> $=dd just does $IFS-splitting. If $dd is meant to contain shell code,
> you should use eval to evaluate it. But to store code, you
> generally use functions not variables. The z and Q parameter
> expansion flags can do the same tokenisation and quote removal
> as the shell syntax parser does, but I don't think you want to
> go there.
I've never had anything but grief in the Q continuum.  Hard to express 
it, but it all seems so .... ad hoc, vague, Rube Goldberg, full of 
gotchas -- (qqq) doesn't work on Mondays -- impossible to remember, 
forced ... but your solutions above are crisp, and understandable and 
rememberable.

Thanks gentlemen both.




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

* Re: preserving single quotes
  2022-09-28 14:32   ` Ray Andrews
@ 2022-09-28 18:49     ` Stephane Chazelas
  2022-09-28 19:29       ` Ray Andrews
  0 siblings, 1 reply; 6+ messages in thread
From: Stephane Chazelas @ 2022-09-28 18:49 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

2022-09-28 07:32:41 -0700, Ray Andrews:
[...]
> > dd="aptitude search '?name(libreoffice-java-common)'"
> > ee=$(eval -- $dd)
> My old friend eval.  But Bart advises to get away from it.  Anyway it works
> for now.
> 
> > Or:
> > 
> > dd=( aptitude search '?name(libreoffice-java-common)' )
> > # or
> ...
> > ee=$( "$dd[@]" )
> Works.  But that puzzles me, it seems simplest, but how is that that saving
> the aptitude string as an array preserves the single quotes? It seems
> unintuitive that the method of saving the string would make the difference.
[...]

There seems to be some confusion as to the role of quotes there.

Here you *do* want to remove the quotes, not preserve them.

aptitude search '?name(libreoffice-java-common)'

is code in the zsh language (or in that case most shell
languages).

That tells the shell: find a command called "aptitude" in the
directories in $PATH and run it in a child process with 3
arguments:

- aptitude
- search
- ?name(libreoffice-java-common)

To do the same thing in another language, like perl, you'd do:

system("aptitude", "search", "?name(libreoffice-java-common)");

In either case, the quotes (and "system", "(", "," or spaces)
are not or the arguments passed to the command, they're just
part of the language (zsh / perl).

dd="aptitude search '?name(libreoffice-java-common)'"

is also code in the zsh language that says store in the $dd
variable: aptitude search '?name(libreoffice-java-common)'
(including the spaces and single quotes, not double quotes which
again are part of the zsh syntax to remove the special meaning
of the space, ', ?, (, ) characters).

$=dd

is again code in the zsh language that says:
- take the value of $dd
- split it on $IFS characters to get a number of words
- derive the command to run from the first word and pass all the
  words to it as arguments.

So assuming the default value of $IFS, that will call
/usr/bin/aptitude with these 3 arguments:

- aptitude
- search
- '?name(libreoffice-java-common)' (including the 's!)

aptitude search terms do understand single quotes as quoting
operators, so instead of searching for packages whose name
contains libreoffice-java-common, you're search for packages
whose name (since name search is the default) contains
?name(libreoffice-java-common), same as if you had run:

"aptitude" "search" "?name('~name(libreoffice-java-common)')"

dd=(aptitude search '?name(libreoffice-java-common)')

is shell code that says store in the dd array 3 elements:

- aptitude
- search
- ?name(libreoffice-java-common)

"${dd[@]}"

Is shell code that says: expand to all the elements of the $dd
array as separate words that will make up the list of arguments
to the command (identified by the first element).

-- 
Stephane


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

* Re: preserving single quotes
  2022-09-28 18:49     ` Stephane Chazelas
@ 2022-09-28 19:29       ` Ray Andrews
  0 siblings, 0 replies; 6+ messages in thread
From: Ray Andrews @ 2022-09-28 19:29 UTC (permalink / raw)
  To: zsh-users

On 2022-09-28 11:49, Stephane Chazelas wrote:
> There seems to be some confusion as to the role of quotes there. 

Thanks for that.  Very easy for subtle misunderstandings to creep in.  
Yes, I am/was thinking of the single quotes as 'part of aptitude' if you 
get  me.  Important to understand that they remain zsh's way of grouping 
the words into three logical arguments.  That thought would never have 
occurred to me. Actually even as I type this, clouds are parting.  That 
also explains why the array syntax obviates the single quotes -- the 
fact of the array already groups the words into three entities.  The 
hard thing is that all this grouping /splitting is invisible.  It's very 
astute of you to catch that I'm getting the invisible stuff wrong.  And 
such clarity of explanation!



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

end of thread, other threads:[~2022-09-28 19:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-28  3:12 preserving single quotes Ray Andrews
2022-09-28  9:10 ` Roman Perepelitsa
2022-09-28 10:00 ` Stephane Chazelas
2022-09-28 14:32   ` Ray Andrews
2022-09-28 18:49     ` Stephane Chazelas
2022-09-28 19:29       ` Ray Andrews

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