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

Re: subscript not working as "I" expected it would



[cc:zsh-users]

On Sat, Nov 4, 2023 at 9:47 AM Roman Perepelitsa
<roman.perepelitsa@xxxxxxxxx> wrote:
>
> On Sat, Nov 4, 2023 at 8:09 AM Jim <linux.tech.guy@xxxxxxxxx> wrote:
> >
> > After beating my head for a while, it dawned on me to try quoting
> >   print -- ${"$(<.git/${(s.: .)$(<.git/HEAD)[2]})"[1,9]}
> > which worked, returning the first 9 characters of the hash.
> >
> > Could someone explain why the quotes are needed here so hopefully
> > the next time I will understand. I swear zsh quoting will drive me nuts.
>
> $(list) expands to an array. ${$(list)[N]} gives the Nth element of the array.
>
> % print -r -- ${$(print foo bar)[1]}
> foo
> % print -r -- ${$(print foo bar)[2]}
> bar
>
> $(list) can be an array with just one element but it's still an array.
> This is the case in your code snippet.
>
> "$(list)" on the other hand is a scalar. ${"$(list)"[N]} gives the Nth
> character.
>
>     % print -r -- ${"$(print foo bar)"[1]}
>     f
>     % print -r -- ${"$(print foo bar)"[2]}
>     o
>
> Roman.




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