LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 02-18-2009, 04:59 PM   #1
talanis
LQ Newbie
 
Registered: Feb 2009
Posts: 4
Thanked: 0
bash - loop over variable array names


[Log in to get rid of this advertisement]
Hello,

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.
Quote:
#!/bin/bash

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

done


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++ ));
do
echo ${array$y[*]} |sed s/"533862"/" "/g
done
Since i need to loop over the output, so i really only get the lines that where filled, any help is really appreciated!
talanis is offline  
Tag This Post , , ,
Reply With Quote
Old 02-18-2009, 07:35 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,497
Thanked: 44
Code:
#!/bin/bash

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

echo "Printing them from a loop:"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~"
for (( y=0; y<3; y++ )); do
    echo $(eval echo \${array$y[*]})
done
Hko is offline     Reply With Quote
Thanked by:
Old 02-19-2009, 12:09 PM   #3
talanis
LQ Newbie
 
Registered: Feb 2009
Posts: 4
Thanked: 0

Original Poster
Thanks man!
It works like a charm. Perfect ^^
talanis is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 08:24 AM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-03-2009 12:18 AM
bash/sh: global or local variable with for (loop) isssue frenchn00b Programming 9 11-06-2008 08:24 AM
BASH Using varaibles as variable names SwingingSimian Programming 5 09-02-2008 03:56 AM
Bash: how to test for a number in Array within if loop? realos Programming 1 12-15-2006 08:59 AM


All times are GMT -5. The time now is 03:38 PM.

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