LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-09-2006, 06:48 PM   #1
thekillerbean
Member
 
Registered: Jan 2002
Location: Melbourne, Australia
Distribution: Ubuntu 22.04 (Jammy)
Posts: 92

Rep: Reputation: 16
Retrieving from a variable whose name is inside a variable.


I've got myself in a situation of which I'm in a quandary!

I've got a list of variables in a script as follows:
var1=txt1
var2=txt2
var3=txt3
var4=txt4
...
varn=txtn

count=n

I've got a counter incrementing a count variable and I'm assigning this count variable to another variable as follows:

y="var$count"

I'm now trying to retrieve the "txt." contents by:

echo $y

but that is not working. What will?

Please help!

Cheers,
kb.
 
Old 02-09-2006, 07:02 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try:
Code:
eval echo \$$y
 
Old 02-09-2006, 07:25 PM   #3
thekillerbean
Member
 
Registered: Jan 2002
Location: Melbourne, Australia
Distribution: Ubuntu 22.04 (Jammy)
Posts: 92

Original Poster
Rep: Reputation: 16
Talking

You freaking genius!

Thank you so much.

I wish I could buy you a real cold beer!

Cheers,
kb.
 
Old 02-09-2006, 07:58 PM   #4
thekillerbean
Member
 
Registered: Jan 2002
Location: Melbourne, Australia
Distribution: Ubuntu 22.04 (Jammy)
Posts: 92

Original Poster
Rep: Reputation: 16
One quick question - how can I assign the contents to another variable?

Cheers,
kb.
 
Old 02-09-2006, 08:50 PM   #5
thekillerbean
Member
 
Registered: Jan 2002
Location: Melbourne, Australia
Distribution: Ubuntu 22.04 (Jammy)
Posts: 92

Original Poster
Rep: Reputation: 16
Never mind - figured it out
var=$(eval echo \$$y)

Cheers,
kb.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe inside variable isn't working in bash Reginald0 Linux - General 6 12-09-2015 09:56 AM
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 05:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:00 PM.

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