LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-14-2014, 06:24 PM   #1
larrycohen28
LQ Newbie
 
Registered: Dec 2013
Posts: 1

Rep: Reputation: Disabled
Expanding a variable within a variable


I've looked around for days and have not been able to find the exact answer I'm looking for.

Here's what I'm trying to do. I have a list which I've put into an array.

list=(a b c d)

I want to be able to use the values in this list in other variables. For example

${${list[0]}_test}=0

I want this to expand to a_test=0

I know this is the incorrect syntax, but I've tried everything I know including eval, but I'm not sure that is applicable here, or if what I am trying to do is even possible.

Would someone be able to either give me the correct syntax or help me find the correct way to do this?

Thanks!

Larry
 
Old 01-14-2014, 11:18 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
This is called indirect expansion but it is not quite as powerful as you would like.

See bash man page Parameter Expansion.

Quote:
Originally Posted by bash man page
If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
So basically you can substitute a variable in a variable. Except only the whole value of that variable can be the replacement. Commands I type start with a $ otherwise it is output.

e.g.

Code:
$ a="b"
$ b="c"
$ echo "${a}"
b
$ echo "${!a}"
c
$ d=(x y z)
$ x="hello"
$ echo "${d[0]}"
x
$ echo "${!d[0]}"
hello
$ echo "${!d[@]}"
0 1 2
However if you attempt echo "${!a_test}" there will be a null value for the variable indirect expansion because a_test as a whole is not a variable.

SAM

Last edited by sag47; 01-14-2014 at 11:25 PM.
 
Old 01-15-2014, 01:51 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
It appears to me you need to create 'test' as an associative array so you can use text as indices:
Code:
declare -A test

test["x"]=blah

echo ${test["x"]}
I have to say, and this no indictment of the OP, but I am amused how many people want to join the output of one variable with other text to make a new variable.
There may be other languages that can do this but to date I have not come across one. Plus for the rest of your code you would need to call the original variable every time you wish
to refer to this new variable ... not sure I see the upside?? (just a thought)
 
Old 01-15-2014, 02:10 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
this will work:
Code:
v=${list[1]}_test
eval ${v}="new value"
echo $b_test
but as it was mentioned already several times usage of eval can be unsafe, therefore not suggested.
I agree with grail, you need to find another construct, for example associative array instead
 
  


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
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 01:48 PM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
expanding variable in shell script dipenchaudhary Programming 8 02-08-2006 05:05 PM
Expanding shell variable in sed goofyheadedpunk Programming 4 09-11-2004 05:17 PM

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

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