LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-06-2006, 01:50 AM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 32
Bash scripting: how can I reference a variable?


Hi everyone,

I have some variables whose names I generated when going through the loop.

for i in `seq 1 10`
do
${i}_title=some_string
done

Now I want to make references to the ${i}_title variables in another loop. How do I do that? I guess my question is how to do nested references to a variable. Thanks.

Last edited by frankie_DJ; 10-06-2006 at 01:58 AM.
 
Old 10-06-2006, 02:19 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The construct
${i}_title
can only be used to create a string value, not a variable name
You need to look at arrays tldp.org/LDP/abs/html/arrays.html
 
Old 10-06-2006, 02:38 AM   #3
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
I thought about that. However, in my second loop I am going through another array. So, if I use array here, then I would have to loop through two arrays simultaneously, using the same index. This would be trivial in any other language but is it possible in bash?

EDIT: Sorry if this is confusing. I think I figured out how to get it working with arrays. Thanks

Last edited by frankie_DJ; 10-06-2006 at 02:41 AM.
 
Old 10-06-2006, 02:55 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I think I figured out how to get it working with arrays.
Well, then share it with your fellow LQ members, or do you want to keep everything for yourself?
 
Old 10-06-2006, 05:13 AM   #5
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
OK well in my first loop I am stepping through integers
1 through 11

Code:
for i in `seq 1 11`
do
Then, rather then generating title with

${i}_title=some_string

(which cannot later be referenced as a variable, so it's useless to me) I generate an array "title"
Code:
title[$i]=some_string
In my second loop I am stepping through members of a different array, which I also made to be integers (not necessarily consecutive).
Code:
for i in ${some_other_array[@]}
do
This is convinient b/c now I can reference "title" array inside of this for loop:
Code:
${title[$i]}
 
  


Reply



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
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM
A book/Online reference for shell scripting waelaltaqi Linux - General 8 03-08-2006 12:19 PM
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
shell scripting: variable problem XST1 Programming 7 01-30-2005 01:45 PM
reference Variable in C++ atul_mehrotra Programming 6 10-06-2004 10:49 AM

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

All times are GMT -5. The time now is 11:19 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