Indirect variables in Bash | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Indirect variables in Bash

Shell Programming and Scripting


Tags
bash, indirect, variable name change

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-11-2013
DFr0st DFr0st is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 13 February 2013, 8:50 AM EST
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
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}=50

However, 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  
Old 02-11-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
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  
Old 02-11-2013
DFr0st DFr0st is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 13 February 2013, 8:50 AM EST
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
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  
Old 02-11-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 31 October 2014, 10:07 PM EDT
Location: Amsterdam
Posts: 9,566
Thanks: 286
Thanked 2,432 Times in 2,179 Posts
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  
Old 02-11-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 October 2014, 9:36 PM EDT
Location: Saskatchewan
Posts: 19,745
Thanks: 832
Thanked 3,375 Times in 3,162 Posts
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  
Old 02-11-2013
DFr0st DFr0st is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 13 February 2013, 8:50 AM EST
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
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  
Old 02-11-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 October 2014, 9:36 PM EDT
Location: Saskatchewan
Posts: 19,745
Thanks: 832
Thanked 3,375 Times in 3,162 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 12:31 AM.