help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] array indirection


From: Dan Douglas
Subject: Re: [Help-bash] array indirection
Date: Fri, 04 May 2012 19:39:43 -0500
User-agent: KMail/4.8.3 (Linux/3.3.4-pf+; KDE/4.8.3; x86_64; ; )

On Friday, May 04, 2012 06:20:46 PM Bill Gradwohl wrote:

Can anyone explain why one pass of this loop works and then fails.

If it didn't work at all, or worked all the time I could understand, but working once and then quitting is confusing.

array=(11 12 13 14 15)
variableName='array'

for ((x=0; x<address@hidden; ++x)); do
   echo "array[${x}]=${!variableName[$x]}"
done
==================
Produces:
address@hidden 01.ycc# ./tst pass3
array[0]=11
./tst: line 33: !variableName[$x]: unbound variable


--
Bill Gradwohl

Don't loop over arrays that way. Either use a `for in' loop to iterate elements, or iterate keys. Also, indirection can't be done that way. There is no such expansion as "${!arrname[x]}" where x is anything other than @ or *.

I wrote an example of how this can be done here:

 

http://wiki.bash-hackers.org/syntax/arrays#indirection

 

Which demonstrates Bash's indirection operator and obtaining keys if necessary.

--

Dan Douglas

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

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