zsh-users
 help / color / mirror / code / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Ray Andrews <rayandrews@eastlink.ca>, zsh-users@zsh.org
Subject: Re: Belaboring substitution syntax
Date: Thu, 17 Nov 2022 09:59:40 +0100	[thread overview]
Message-ID: <CAN=4vMovAvSK8QOJ-uMiK6ujdP+vvBF+d3SUHyX-oLNQaA30cg@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7ZFoj+0okm1EUZEsb8XqDarrSu9WDNiDxJ-Y5T-_ThD3A@mail.gmail.com>

On Thu, Nov 17, 2022 at 3:32 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Wed, Nov 16, 2022 at 6:15 AM Roman Perepelitsa
> <roman.perepelitsa@gmail.com> wrote:
> >
> > On Wed, Nov 16, 2022 at 3:09 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
> > >
> > > "${array[@]}" ... Read: protect the elements of this array, do any expansions within them, but do NOT combine them ??
> >
> > No, this just expands the array to all its elements.
>
> In the base cases, you can think of it as "if I can't see it, nothing
> happens to it."  The exceptions only occur when you start adding
> parameter flags like ${(e)array[@]}.

If I didn't know that ${array[@]} without quotes does not in general
expand to the array's elements, I could be confused by this exchange,
so I'll post some examples for Ray's benefit.

This does nothing (the array's content is unchanged):

    array=("${array[@]}")

This may change the content of the array:

    array=(${array[@]})

The content of the array is changed by this statement in two ways:

1. All empty elements are removed.
2. If sh_word_split is set, elements of the array are split on $IFS.

To demonstrate (1):

    % () {
      emulate -L zsh
      local array=('' 'foo bar')
      typeset -p array
      array=(${array[@]})
      typeset -p array
    }

    typeset -a array=( '' 'foo bar' )
    typeset -a array=( 'foo bar' )

To demonstrate (2):

    % () {
      emulate -L zsh -o sh_word_split
      local array=('' 'foo bar')
      typeset -a array=( 'foo bar' )
      array=(${array[@]})
      typeset -p array
    }

    typeset -a array=( '' 'foo bar' )
    typeset -a array=( foo bar )

In short, "${array[@]}" expands to array elements just like "$scalar"
expands to the scalar's value. Versions without quotes are more
complex, for they may transform the value in various ways.

Additionally, unless ksh_arrays is set (it's unset by default), these
two are equivalent:

    "${array[@]}"
    "${(@)array}"

And these four are equivalent:

    ${array[@]}
    ${(@)array}
    ${array}
    $array

Roman.


  reply	other threads:[~2022-11-17  9:00 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-10  0:43 coloring substitution seems to eat next line Ray Andrews
2022-11-10  8:02 ` Roman Perepelitsa
2022-11-10 18:25   ` Ray Andrews
2022-11-10 18:36     ` Roman Perepelitsa
2022-11-10 18:45       ` Ray Andrews
2022-11-10 18:54       ` Bart Schaefer
2022-11-10 19:28         ` Ray Andrews
2022-11-10 20:22           ` Bart Schaefer
2022-11-10 21:42             ` Ray Andrews
2022-11-10 21:51               ` Roman Perepelitsa
2022-11-11 17:24                 ` Ray Andrews
2022-11-10 22:47               ` Bart Schaefer
2022-11-10 23:07                 ` Ray Andrews
2022-11-10 23:27                   ` Bart Schaefer
2022-11-11 15:00                     ` Ray Andrews
2022-11-11 18:15                       ` Bart Schaefer
2022-11-11 18:50                         ` Ray Andrews
2022-11-11 19:25                           ` Bart Schaefer
2022-11-11 21:26                             ` Ray Andrews
2022-11-12  4:24                               ` Bart Schaefer
2022-11-12 14:03                                 ` Ray Andrews
2022-11-13 15:09                                   ` Ray Andrews
2022-11-14 14:12                                     ` Roman Perepelitsa
2022-11-14 17:08                                     ` Ray Andrews
2022-11-14 17:12                                       ` Roman Perepelitsa
2022-11-14 18:58                                         ` Ray Andrews
2022-11-14 20:00                                           ` Bart Schaefer
2022-11-14 23:25                                             ` Ray Andrews
2022-11-15 14:17                                               ` Belaboring substitution syntax Ray Andrews
2022-11-16  1:49                                                 ` Bart Schaefer
2022-11-16  2:54                                                   ` Ray Andrews
2022-11-16  6:26                                                     ` Bart Schaefer
2022-11-16 14:08                                                       ` Ray Andrews
2022-11-16 14:13                                                         ` Roman Perepelitsa
2022-11-17  2:31                                                           ` Bart Schaefer
2022-11-17  8:59                                                             ` Roman Perepelitsa [this message]
2022-11-17 16:02                                                               ` Ray Andrews
2022-11-16 20:46                                                       ` Ray Andrews
2022-11-16 10:32                                                   ` Roman Perepelitsa

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='CAN=4vMovAvSK8QOJ-uMiK6ujdP+vvBF+d3SUHyX-oLNQaA30cg@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=rayandrews@eastlink.ca \
    --cc=schaefer@brasslantern.com \
    --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).