|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Indirect variables in Bash
Hello, I've spent hours this morning reading various past forum posts and documentation pages but I can't find exactly what I need. I'm trying to call a variable with a variable in the name without having to make a third variable. For example: Code:
path=AB legAB=50 leg$path I want to be able to call leg$path variable and get the value "50" I am aware that if I created another variable I could do this using the ${! } notation: Code:
var=leg$path
echo ${!var}=50However, I want to be able to miss out this last step. Is this possible? Thanks, Dan Last edited by DFr0st; 02-11-2013 at 07:05 AM.. |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
A not-recommended way: Code:
eval echo \$leg$path By the way, I think you meant echo ${!var} instead of echo ${!leg} . |
| The Following User Says Thank You to elixir_sinari For This Useful Post: | ||
DFr0st (02-11-2013) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Hi,
Yep, that works. Why is it not recommended? You were right, I made a mistake in the original post but it has now been changed. |
|
#4
|
||||
|
||||
|
Because if
$path is obtained from user input or an input file, or otherwise not under your control someone could execute code with the permissions of the person executing the script. If you are using bash 4 or you could switch to ksh93 then an alternative might be associative arrays: Code:
leg=([AB]=50 [CD]=40)
echo "${leg[AB]}" |
| The Following User Says Thank You to Scrutinizer For This Useful Post: | ||
DFr0st (02-11-2013) | ||
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
This is easiest done with read. It's handy because it takes a variable name, which you can construct by any means you want. Code:
read $A$B <<<"string" |
| The Following 2 Users Say Thank You to Corona688 For This Useful Post: | ||
DFr0st (02-11-2013), Scrutinizer (02-11-2013) | ||
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Thanks Scrutinizer and Corona. However, how do you implement "read" to do this? I've been playing with it and reading about it but can't find anything about this usage.
|
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Kind of exactly how I've shown you. Code:
A="abc" B="def" read $A$B <<<"string" ...should put the string "string" into the variable named abcdef. This is how it's often used: Code:
read VARNAME ...but note that it takes a variable name -- a string -- not the variable itself. You can feed it whatever string you please, derived from whatever variables you please, and do things that would otherwise require ugly insecure eval hacks. You can redirect into it any way you please. <<< is an ordinary redirection in BASH which replaces stdin with a string. |
| The Following 2 Users Say Thank You to Corona688 For This Useful Post: | ||
DFr0st (02-11-2013), elixir_sinari (02-11-2013) | ||
| Sponsored Links | ||
|
![]() |
| Tags |
| bash, indirect, variable name change |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Indirect Referral Script | mdlloyd7 | Shell Programming and Scripting | 2 | 02-15-2012 09:09 PM |
| Does SH support indirect expansion like BASH? | dimentiy | Shell Programming and Scripting | 1 | 06-09-2011 12:08 PM |
| How to get an Indirect Variable Value..? | vickramshetty | Linux | 6 | 03-12-2010 01:23 AM |
| Length of an indirect variable | gone_bush | Shell Programming and Scripting | 0 | 03-28-2006 06:17 PM |
|
|