LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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-27-2008, 06:24 PM   #1
dovo
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Rep: Reputation: 0
bash scripting need help getting a variable of a variable


I'm not sure what the best way is to do this, but here's my best example

PORT:~$ set JOHN SAM BILL
PORT:~$ name=$3
PORT:~$ echo $name
BILL
PORT:~$ number=3
PORT:~$ name="$"${number}""
PORT:~$ echo $name
$3

If it will make a difference inside of a bash script of not I'm unsure

the first method gives me the answer I want but the number is being randomly generated, and I need to be able to get the script to pull that number from another variable if there is a better way to do this I'm all ears. I also realize that this is probably the worst application for a bash script, but my question is whether it is or is not possible. I know that C C++ would be better code

Thanks for any suggestions
 
Old 10-27-2008, 06:36 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
I think you need to read this page: http://www.tldp.org/LDP/abs/html/arrays.html
In fact, bookmark the whole doc.
see also
http://rute.2038bug.com/index.html.gz
 
Old 10-28-2008, 04:52 PM   #3
dovo
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Original Poster
Rep: Reputation: 0
even without I get the same result

pcarrick@PORT:~$ set JOHN SAM BILL
pcarrick@PORT:~$ name=$3
pcarrick@PORT:~$ echo $name
BILL
pcarrick@PORT:~$ number=3
pcarrick@PORT:~$ name="$"$number""
pcarrick@PORT:~$ echo $name
$3

maybe I wasn't clear enough

regardless of where the variable is I want to use the value of $number to retrieve $3, I'm not sure if its possible
 
Old 10-28-2008, 05:02 PM   #4
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
???

Code:
$ set JOHN SAM BILL
$ name=$3
$ echo $name
BILL
$ number=3
$ name=$(eval "echo \${$number}")
$ echo $name
BILL
 
Old 10-28-2008, 05:18 PM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
Or you can use indirect substitution:
Code:
bash-3.1$ set JOHN SAM BILL
bash-3.1$ echo $3
BILL
bash-3.1$ name=$3
bash-3.1$ echo $name
BILL
bash-3.1$ number=3
bash-3.1$ echo ${!number}
BILL
This is very handy when you are writing a script and are afraid of what might happen by using 'eval' if the input is unknown.
 
Old 10-29-2008, 02:24 PM   #6
dovo
LQ Newbie
 
Registered: Oct 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks

Thanks that worked perfect.

Two questions is this variables by reference, something else, what is it called?

The other is do the {! symbols work together or are they two separate commands?

Thanks again
 
Old 10-29-2008, 02:30 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The correct name is "indirect variable reference". The two symbols work together. See here for further details.
 
  


Reply


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 scripting -- command saved as variable bioinformatics_guy Linux - Newbie 10 09-09-2008 09:24 PM
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 03:39 PM
bash scripting : printing variable with the number within $i amdGTintel Programming 2 01-18-2007 02:30 AM
Bash scripting: how can I reference a variable? frankie_DJ Programming 4 10-06-2006 06:13 AM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 09:47 PM


All times are GMT -5. The time now is 06:35 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration