set a variable with a variable name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting set a variable with a variable name
# 1  
Old 11-01-2009
Question set a variable with a variable name

Hi there,

I can display a variable with a variable name:
Code:
localhost:~# a=1
localhost:~# b=a
localhost:~# echo $b
a
localhost:~# echo ${!b}
1

But how can I set a variable with a variable name?
In the following example, I want to set a = 1:
Code:
localhost:~# b=a
localhost:~# $b=1
-bash: a=1: command not found

Can anyone help me?
Thanks in advance
Santiago
# 2  
Old 11-01-2009
Hi.

Using eval:

Code:
b=a
eval $b=1
echo $a
1

# 3  
Old 11-01-2009
Quote:
Originally Posted by chebarbudo
Hi there,

I can display a variable with a variable name:
Code:
localhost:~# a=1
localhost:~# b=a
localhost:~# echo $b
a
localhost:~# echo ${!b}
1


Note that that is not portable; it is specific to bash.

The portable syntax is:

Code:
eval "echo \${$b}"

Quote:
But how can I set a variable with a variable name?
In the following example, I want to set a = 1:
Code:
localhost:~# b=a
localhost:~# $b=1
-bash: a=1: command not found

Code:
eval "$b=1"

# 4  
Old 11-02-2009
Thanks very much scottn and cfajohnson, it all works.

By the way, I just heard about the usage of bits as rewards for people who help others on the forum. It looks like I already earned some. It's a bit embarassing to ask but what is a reasonable number of bits to reward someone for helping me on that subject?
Thanks for helping me being a better programmer and a more civilized forum user.
# 5  
Old 11-02-2009
You seemed civilised to me anyway ;-)

I try to give bits on how useful I find the answer is to me (even though I didn't ask the question).

Save your bits for a rainy day.

Last edited by Scott; 11-02-2009 at 11:45 AM..
# 6  
Old 11-03-2009
Hey masters,
What's wrong with my syntax?
Code:
r29829:~# cat conf
A=1
B=2
C=3
r29829:~# cat testconf.sh
#!/bin/bash
cat conf | while IFS="=" read lvalue rvalue; do
        eval $lvalue=$rvalue
done
echo "-$A-$B-$C-"
r29829:~# ./testconf.sh
----

I was expecting the following output:
Code:
r29829:~# ./testconf.sh
-1-2-3-

Did I do something wrong?
Thanks for your help.
# 7  
Old 11-03-2009
Code:
#!/bin/bash
while IFS="=" read lvalue rvalue; do
 eval $lvalue=$rvalue
done < conf
echo "-$A-$B-$C-"


-1-2-3-


Last edited by Scott; 11-03-2009 at 06:56 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

do you have a better way to set this variable?

greetings, i have a variable $input that i want to use to set $output. $input is /dir/filename.mph and $input is passed to my script that i manipulate it as follows: input=`basename $input`i want the $output to be filename_solved.mph, basically stuffing "_solved" in the filename. here's how i... (2 Replies)
Discussion started by: crimso
2 Replies

2. Shell Programming and Scripting

How to know who and where a variable is set ?

hi, i'm not a root user and i want to know which user and in which file is loaded a variable seen in the "env" display ? I will use this variable but i want to be sure that it will be a permanent variable ! i don't see it in my files (.profile , kshrc...) and neither in /etc/profile. ... (3 Replies)
Discussion started by: Nicol
3 Replies

3. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

4. UNIX for Dummies Questions & Answers

How to set a variable with a count variable i.e. VARIABLE$COUNT

Hi All I've very nearly finished this script I'm working on but have hit another idiots problem, after googling I can't see a solution for this one. I have a while count loop that checks checks two consecutive values then increments the count by two. What the script has to do is then check... (5 Replies)
Discussion started by: Bashingaway
5 Replies

5. Shell Programming and Scripting

Set variable with variable

Okay, probably an easy solution, but I've given up. I want to set a variable with a variable and print it in PHP. I just can't get the syntax right. As an example: $place = "T"; $norm_t = array("4","7"); I want to use this in a for-loop. How do I echo the first number of the norm_t... (4 Replies)
Discussion started by: wxornot
4 Replies

6. Shell Programming and Scripting

set variable in while loop?

Dear All, Can anyone advise why this script isn't run as expected? =========================== status=0 cat /etc/passwd | while read line; do status=1 done echo $status =========================== it always return 0 , but not 1. why? anything wrong? Thanks. (1 Reply)
Discussion started by: tiger2000
1 Replies

7. Shell Programming and Scripting

global variable not being set

In ksh I thought a global variable was any variable in a script or function that did not have the typeset command. I have a global in my calling script which I increment in a function, but the value does not change in the calling script. Here is the code: function f_open_log { typeset -r... (5 Replies)
Discussion started by: robotball
5 Replies

8. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies

9. Linux

How do i set environment variable

Hi, I am quite new to Linux. And I have doubt how to set new environment variable with value to a C executable. Let say I have a environment variable $Hack ; I would like to load a value for this variable; so that when the C executable is executed, the $Hack would set the variable value. ... (4 Replies)
Discussion started by: ahjiefreak
4 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question