help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use of $@


From: alex xmb ratchev
Subject: Re: Use of $@
Date: Tue, 21 Feb 2023 13:57:12 +0100

On Tue, Feb 21, 2023, 1:35 PM Christof Warlich <cwarlich@gmx.de> wrote:

> Am 21.02.23 um 12:06 schrieb Koichi Murase:
>
> > It is easy to rewrite the shell function `index' using shift as
> > Andreas suggests,
>
> Yes, that's probably the solution that I'm going to choose :-).
>
> And, by the way, also thanks to Kerin for pointing out the issue with
> sparse arrays, although my use case (currently) would not be affected.
>
> > but I assume your true question is about the way to
> > access $@ with a specified index. You can use ${!i}. Or if you would
> > like to extract multiple words in a range, you can use
> > "${@:i:length}".
>
> 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
>

   l() { declare -i i=1
  while (( ++i <= $# )) ; do
 [[ ${!i} == "$1" ]] &&
printf $i\\n &&
return
  done
   }

l abc def abc def
3

Can anyone spot what's wrong?
>
> >
> > --
> > Koichi
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]