Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-18-2009, 03:59 PM   #1
LQ Newbie
Registered: Feb 2009
Posts: 4

Rep: Reputation: 0
bash - loop over variable array names


i have a little problem concerning variable variable names in bash.
I am doing a loop to fill multiple arrays.

if i want to output the arrays, i can, however, do it only one by one; not in another loop.

for (( n=0; n<5; n++ )); # create 5 arrays
let array$n[0]="432859"


echo ${array0[*]} # arrays 0 to 4 can be easily displayed.
echo ${array1[*]} # but i want it as a loop (necessary in mightier programs)
echo ${array2[*]} # and so on...

#this loop does not work.
#line 16: ${array$y[*]}: bad substitution
for (( y=0; y<$n; y++ ));
echo ${array$y[*]} |sed s/"533862"/" "/g
Since i need to loop over the output, so i really only get the lines that where filled, any help is really appreciated!
Old 02-18-2009, 06:35 PM   #2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110

array0=(Hello Hallo Hola Aloha)
array1=(one two three four)
array2=(1 2 3 4)

echo "Printing them the normal way:"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo ${array0[*]}
echo ${array1[*]}
echo ${array2[*]}

echo "Printing them from a loop:"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~"
for (( y=0; y<3; y++ )); do
    echo $(eval echo \${array$y[*]})
1 members found this post helpful.
Old 02-19-2009, 11:09 AM   #3
LQ Newbie
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks man!
It works like a charm. Perfect ^^


array, bash, substitution, variables

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: Reading long filenames into an array using a loop DaneM Programming 12 09-11-2009 07:24 AM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
bash/sh: global or local variable with for (loop) isssue frenchn00b Programming 9 11-06-2008 07:24 AM
BASH Using varaibles as variable names SwingingSimian Programming 5 09-02-2008 02:56 AM
Bash: how to test for a number in Array within if loop? realos Programming 1 12-15-2006 07:59 AM

All times are GMT -5. The time now is 01:08 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration