zsh-users
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: zsh-users@zsh.org
Subject: Re: preserving single quotes
Date: Wed, 28 Sep 2022 19:49:39 +0100	[thread overview]
Message-ID: <20220928184939.46gftnscdz22e5m3@chazelas.org> (raw)
In-Reply-To: <0ede34f7-4684-8b48-700e-9555cccfa693@eastlink.ca>

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


  reply	other threads:[~2022-09-28 18:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-28  3:12 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 [this message]
2022-09-28 19:29       ` Ray Andrews

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=20220928184939.46gftnscdz22e5m3@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@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).