Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: preserving single quotes



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




Messages sorted by: Reverse Date, Date, Thread, Author