LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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


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

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


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!
 
Old 02-18-2009, 06:35 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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
 
1 members found this post helpful.
Old 02-19-2009, 11:09 AM   #3
talanis
LQ Newbie
 
Registered: Feb 2009
Posts: 4

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


Reply

Tags
array, bash, substitution, variables



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration