LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 03-18-2010, 05:44 PM   #1
youarefunny
Member
 
Registered: Mar 2010
Distribution: Arch Linux, FreeBSD
Posts: 31

Rep: Reputation: 0
Bash: Making a Variable out of two other variables.


I need to find the value of:
Code:
$Namenumber
My script asks for the name you want to look up and I want it to return the value of $Namenumber

I was thinking:
Code:
number=$"$name"number
but this returns
Code:
$Namenumber
but does not actually resolve what the variable $Namenumber is equal to.

How would you do this?
Thank you
 
Old 03-18-2010, 06:04 PM   #2
primerib
Member
 
Registered: Mar 2010
Posts: 38

Rep: Reputation: 19
I'm not sure what exactly you're asking but if you just want to join $name and $number into a single variable then:
number="$name$number"

If you just want to know what $Namenumber holds then:
echo "$Namenumber"

Otherwise can you restate your question so it's more clear?

Last edited by primerib; 03-18-2010 at 06:05 PM.
 
Old 03-18-2010, 07:32 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
I am guessing you are looking for an indirect reference, so you need one more step:

Code:
timnumber=10
name="tim"
namenumber="${name}number"

echo "${!namenumber}" # or eval echo \$$namenumber
 
Old 03-29-2010, 04:30 PM   #4
youarefunny
Member
 
Registered: Mar 2010
Distribution: Arch Linux, FreeBSD
Posts: 31

Original Poster
Rep: Reputation: 0
Thankyou

The secret is eval echo.

Code:
query='$'"$name"number
return=$(eval echo $query)
 
Old 03-29-2010, 06:56 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
Don't forget to mark as SOLVED
 
  


Reply

Tags
bash, variable, variables


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
-bash: HISTSIZE: readonly variable -bash: HISTFILESIZE: readonly variable deathsfriend99 Linux - Newbie 4 12-08-2009 12:51 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Variable names that include variables.... mychl Programming 7 09-24-2003 07:13 AM


All times are GMT -5. The time now is 03:45 AM.

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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration