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

Re: [Review Request] Arrays and their usage

On 5/31/21, René Neumann <lists@xxxxxxxxx> wrote:
> Hi *,
> I always feel a little unsure around arrays in zsh. I've currently used
> the following:
>     local pkgs=( `makepkg --printsrcinfo | \
>                   sed -n -e 's/pkgname = \(.*\)$/\1/p'` )
>     pkgs=(${pkgs/#/"$DATABASE/"})
>     sudo pacman -S $pkgs
> Intention: Generate a list of packages, prepend "$DATABASE/", and pass
> each modified package as a separate argument to pacman.
> Question: Is this the correct/zshonic way of doing this?
> I personally find the change of behavior by adding ( ) too easy to
> overlook. Is there an alternative with ${(...)}?

I don't know what the exact output of your makepkg pipeline can be,
but unquoted `` will split words on all whitespace, so that is a bit
of a danger. I would probably have written it like this

local pkgs=( ${(f)"$(makepkg --blabla | sed blabla)"} )
sudo pacman -S $DATABASE/$^pkgs

That's assuming that the packages from the pipeline are separated by newlines.

Mikael Magnusson

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