The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
assign awk's variable to shell script's variable? tiger2000 Shell Programming and Scripting 3 3 Weeks Ago 04:47 AM
Insert a line including Variable & Carriage Return / sed command as Variable lowmaster Shell Programming and Scripting 2 05-19-2009 11:26 PM
Sed variable substitution when variable constructed of a directory path alrinno Shell Programming and Scripting 2 07-11-2008 02:24 PM
Enviornment Variable in B shell (I call it nested variable) princelinux Shell Programming and Scripting 4 07-02-2008 01:35 AM
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 08:28 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
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 (permalink)  
Old 3 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,046
Hi.

Using eval:

Code:
b=a
eval $b=1
echo $a
1
Bits Awarded / Charged to scottn for this Post
Date User Comment Amount
3 Weeks Ago chebarbudo Thanks for your help 100
  #3 (permalink)  
Old 3 Weeks Ago
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by chebarbudo View Post
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"
Bits Awarded / Charged to cfajohnson for this Post
Date User Comment Amount
3 Weeks Ago chebarbudo A very complete answer, thanks for your time 150
  #4 (permalink)  
Old 3 Weeks Ago
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
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 (permalink)  
Old 3 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,046
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 scottn; 3 Weeks Ago at 11:45 AM..
  #6 (permalink)  
Old 2 Weeks Ago
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
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 (permalink)  
Old 2 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,046
Code:
#!/bin/bash
while IFS="=" read lvalue rvalue; do
 eval $lvalue=$rvalue
done < conf
echo "-$A-$B-$C-"


-1-2-3-

Last edited by scottn; 2 Weeks Ago at 06:56 PM..
Reply

Bookmarks

Tags
variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0