Getting value of variable whose name is stored in another variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting value of variable whose name is stored in another variable
# 1  
Old 07-18-2013
Getting value of variable whose name is stored in another variable

Unix OS : Linux 2.6x
Shell type : Korn
I am stuck in weird problem .
In my shell script I am setting an environment variable using the following command :
Code:
EMP="KUMARJIT"; export EMP

In the following sections of the script , what I did is :
I created and initialized a new shell variable "type" using the following command:
Code:
type="EMP"

As because the VALUE OF A SHELL VARIABLE(type) IS THE NAME OF ONE THE ENVIRONMENT VARIABLE(EMP) ,
how can I get the value of variable "EMP" using the shell variable "type" ?


Thanks
Kumarjit.
# 2  
Old 07-18-2013
Linux Getting value of variable whose name is stored in another variable

try this....Smilie

Code:
type=${EMP}

# 3  
Old 07-18-2013
It's not exactly obvious what you want, but...
Code:
$ eval echo \$$type
KUMARJIT

PS: Linux 2.6 is not an operating system, Unix or otherwise!
# 4  
Old 07-18-2013
Were it bash, you could use its "variable indirection":
Code:
echo ${!type}
KUMARJIT

In ksh, you could use the ill reputed eval:
Code:
eval echo \$$type
KUMARJIT

# 5  
Old 07-18-2013
@Scot and Rudic : Thanks guys for your WONDER replies . It clicked great time .

regards
Kumarjit.
# 6  
Old 07-18-2013
The Korn shell that comes with your OS is ksh93. It should be able to do this:
Code:
EMP=KUMARJIT
nameref type=EMP
echo "$type"
KUMARJIT

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to echo a value of a var stored in another variable?

I'm writing a shell script in AIX and using ksh. I have a scenario where I have a variable A which stores $B. so when i echo "$A" it prints $B But I wish to print value stored in var b ie. \a\dir\res\ I wish to store \a\dir\res\ in a third variable C. later I want to cd into that path :... (1 Reply)
Discussion started by: simpltyansh
1 Replies

2. Shell Programming and Scripting

Resolve parameter value stored in a variable

Hi All, I have below variable, xyz=\$AI_XFR Now, if you will run the below command => echo $xyz $AI_XFR It is returning hardcoded string value. Whereas in environment, there is value in it. Like below: => echo $AI_XFR /home/aditya/sandbox/xfr/ I need to resolve this... (4 Replies)
Discussion started by: adgangwar
4 Replies

3. Shell Programming and Scripting

Variable being stored with additional characters

I am trying to store a string value to a temporary variable within my script. When I run the command manually it returns the expected value, but from the script an additional "\r" is at the end. tCurrentStatus=`cat ${tPC_status} | grep 'Current status' | awk '{print $NF}'` output: cat... (1 Reply)
Discussion started by: jxrst
1 Replies

4. UNIX for Dummies Questions & Answers

List of All Users Stored in Variable

I'm trying to get a list of all users on the system, store the list in a variable, then find all the files of a certain extension owned by each of the users. The command I use to get the list is getent passwd | cut -d: -f1 | sortand this works just fine. However, if I try to store it in a... (1 Reply)
Discussion started by: hobscrk777
1 Replies

5. Red Hat

How to check values stored in variable?

hey, i have stored values of query like this val_2=$( sqlplus -s rte/rted1@rel75d1 << EOF set heading off select source_id from cvt_istats where istat_id > $val_1; exit EOF ) echo $val_2 now , val_2 has five values displayed like this 1 2 3 4 5 what i have to do is to check the... (1 Reply)
Discussion started by: ramsavi
1 Replies

6. Shell Programming and Scripting

Function returns a value but cannot be stored in other variable

I need help to store the value returned from the function to one variable and then use that variable. PREVIOUS_DATE_FUNCTION() { date '+%m %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - 1` case "$DAY" in 0) MONTH=`expr "$MONTH" - 1` case... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

7. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

8. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

9. Shell Programming and Scripting

Increment variable stored in a file

Hi, I want to write a perl script, which will increment number stored in file. I want to do this without any file handles. I think we have to use some UNIX commands. I am not sure how to do this with file handles. Thanks, (1 Reply)
Discussion started by: solitare123
1 Replies

10. UNIX for Dummies Questions & Answers

can i exectue a command stored in a variable.it goes like this

hi the code i ve written is as follows: command="egrep ',100,|,200,' | wc -l" count =`cat trial.txt | $command` echo "$count" the contents of trial.txt is : 000,100,200,300, 001,200,100,201, 002,200,100,400, i need to get the count using the command stored in a variable and using... (2 Replies)
Discussion started by: Syms
2 Replies
Login or Register to Ask a Question