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: Greg Wooledge
Subject: Re: how to declare -n from arguments ?
Date: Mon, 8 Jul 2024 19:39:59 -0400

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.

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.

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?



reply via email to

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