Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use the value of a variable for the name of a new variable ? - BASH 1

Status
Not open for further replies.

doublea1535

IS-IT--Management
Feb 1, 2006
30
US
I am trying to use the value of variable "var" as the name for a new variable in BASH. I am not getting it to work. Does anyone know if this can be done somehow?

$~ var=one
$~ echo $var
one
$~ ${var}=two
bash: one=two: command not found
$~ `echo $var`=two
bash: one=two: command not found
$~ $[var]=two
bash: 0=two: command not found
$~ {$var}=two
bash: {one}=two: command not found
 
I think I found the answer. Apparently you can use variables as element IDs. This should work for my purposes, but does anyone know if it is possible to do outside of arrays within BASH?

Code:
root@ensim ~/tst00 : vara=1
root@ensim ~/tst00 : varb=2
root@ensim ~/tst00 : varc=3
root@ensim ~/tst00 : newArray[$vara]=Staceyrocks
root@ensim ~/tst00 : newArray[$varb]=Aaronrocks
root@ensim ~/tst00 : newArray[$varc]=Webothrock
root@ensim ~/tst00 : echo ${newArray[1]}
Staceyrocks
root@ensim ~/tst00 : echo ${newArray[2]}
Aaronrocks
root@ensim ~/tst00 : echo ${newArray[3]}
Webothrock
 
take a look at the eval builtin shell command

var1=var2
eval $var1=two
echo $var2
eval echo \$$var1

man bash for more info

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top