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

Re: [PATCH] Enable further expansion of parameter name by ${!...}



Hi all,
Thank you for your comments.

> IIRC, in bash, you can also use ${!foo} even with banghist active. We
> probably don't want to replicate that feature, but make sure ${\!foo}
> works I suppose?

Right, I don't think we want to change banghist behavior when it is active.
We can avoid banghist by ${\!foo} when it is active.

2015-01-25 16:39 GMT-05:00 Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>:
>
> On Jan 25, 11:38pm, ZyX wrote:
> } Subject: Re: [PATCH] Enable further expansion of parameter name by ${!...}
> }
> } 25.01.2015, 23:25, "Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxx>:
> } >
> } > Don't we already have something like this for ksh emulation?  Except that
> } > it acts like (k) instead of like (P)?  Although I can't find this in the
> } > documentation anywhere, right at the moment.
> }
> } I have ksh installed and here `${!VAR}` outputs `VAR` regardless of
> } whether $VAR is defined and what type and what value does it have.
>
> I thought I recalled something about that.  So it would appear that
> whatever interpretation we're giving it already in ksh mode, is wrong.
>
> } [Manual page][1] says that it should actually expand to "the name of
> } the variable referred to by vname" and says that this will be `VAR`
> } unless it is a name reference (I have only tried strings, arrays and
> } associative arrays).
> }
> } [1]: http://unixhelp.ed.ac.uk/CGI/man-cgi?ksh+1
>

I have tried some patterns with ksh and bash.

### Variable reference
aaa=bbb
bbb=ccc
                    # ksh       bash     zsh(default)       zsh(KSH)
echo $aaa           # bbb       bbb      bbb                bbb
echo ${!aaa}        # aaa       ccc      bad substitution   bbb

### Assoc
typeset -A foo
foo[bar]=baz
foo["baz"]="qux"
baz=piyo
                    # ksh       bash     zsh(default)       zsh(KSH)
echo $foo           #                    baz qux
echo ${foo[bar]}    # baz       baz      baz                baz
echo ${foo["bar"]}  # baz       baz
echo ${foo[baz]}    # qux       qux
echo ${foo["baz"]}  # qux       qux      qux                qux
echo ${!foo[bar]}   # foo[bar]  piyo     bad substitution   bar
echo ${!foo["bar"]} # foo[bar]  piyo     bad substitution
echo ${!foo[@]}     # bar baz   bar baz  bad substitution  "baz" bar

It seems that ${!...} in KSH emulation mode of zsh is only useful for the case
of "${!foo[@]}", which is substituted with the list of keys of the assoc,
that has the same meaning also in bash.
(Although zsh regards baz and "baz", or even 'baz' as different keys...)

According to this, introducing bash-like behaviors instead of "bad
substitution" error will
not hurt zsh even in ksh emulation mode.

Any comments are appreciated.

Thanks,
Tomoki



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