help-bash
[Top][All Lists]
Advanced

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

Re: how to declare -n from arguments ?


From: alex xmb sw ratchev
Subject: Re: how to declare -n from arguments ?
Date: Tue, 9 Jul 2024 01:50:54 +0200

On Tue, Jul 9, 2024, 01:40 Greg Wooledge <greg@wooledge.org> wrote:

> On Tue, Jul 09, 2024 at 00:33:47 +0200, alex xmb sw ratchev wrote:
> > say i have i= , then while (( ++i <= $# ))
> > and here i want the -n way , to test
> > instead n=${!i}
> > for one declare -n before the loop
> >
> > but i cant get any code working ..
> >
> > ~ $ declare -n n=\!i
> > bash: declare: `!i': invalid variable name for name reference
> >
> > declare -n n=@\[i]
> > bash: declare: `@[i]': invalid variable name for name reference
>
> So... you want to iterate over the positional parameters.  And you know
> several ways to do this.  But for some reason, you want to create
> *another* way to do it, using a name reference.
>

it would , if itd have worked , an efficient way ..

hobbit:~$ f() { declare -n n=1; echo "$n"; }; f 1 2 3
> bash: declare: `1': invalid variable name for name reference
>
> hobbit:~$ f() { declare -n n='@[1]'; echo "$n"; }; f 1 2 3
> bash: declare: `@[1]': invalid variable name for name reference
>
> Looks like it doesn't work.  You'll have to use one of the other ways
> that have existed for years and which work perfectly well.
>

seems me so

hobbit:~$ f() { local n; for n; do echo "next: $n"; done; }; f 1 2 3
> next: 1
> next: 2
> next: 3
>
> If you want "n" to refer to the next positional parameter in a loop,
> what's wrong with this way?
>

ill do on func call args=( '' "$@" ) and do args and i in -n

greets ..

>


reply via email to

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