LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: Making a Variable out of two other variables. (https://www.linuxquestions.org/questions/programming-9/bash-making-a-variable-out-of-two-other-variables-796382/)

youarefunny 03-18-2010 05:44 PM

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

primerib 03-18-2010 06:04 PM

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?

grail 03-18-2010 07:32 PM

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


youarefunny 03-29-2010 04:30 PM

Thankyou

The secret is eval echo.

Code:

query='$'"$name"number
return=$(eval echo $query)


grail 03-29-2010 06:56 PM

Don't forget to mark as SOLVED


All times are GMT -5. The time now is 10:33 PM.