On Tue, 21 Feb 2023 13:34:37 +0100
Christof Warlich <cwarlich@gmx.de> wrote:
> Am 21.02.23 um 12:06 schrieb Koichi Murase:
> Nevertheless, thanks a lot for getting back to my _real_ question w.r.t.
> the usage of $@ compared to a named array: I tried your suggestion
> before already, but without success. This is what I tried and what I
> would have expected to work:
>
> $ index() { local e="$1"; shift; for i in "${!@}"; do [[ ${!i} != $e ]]
> || { echo $i; break; }; done; }
> $ LANG=C index "xx yy" "${myarray[@]}"
> bash: a bc xx yy z: invalid variable name
>
> Can anyone spot what's wrong?
To expand "${!@}" is useless, as was just explained in another post.
You could make it work in this particular fashion by writing "for ((i = 0; i <
$#; i++)); do ...; done", though you would have to account for i being
off-by-one. Andreas' suggestion best fits your use case, I'd say.
P.S. Your code still suffers from the bug of not quoting the right-hand side of
== (assuming you want for the value of e to be taken literally).
--
Kerin Millar