LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-11-2011, 02:43 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
How to evaluate the value of a variable ?


How to evaluate the value of a variable ?
For example:

a=var
$a=value !!!error happens!!!

I want to evaluate var=value, how to realize it?
Thanks!
 
Old 08-11-2011, 04:15 AM   #2
16pide
Member
 
Registered: Jan 2010
Posts: 418

Rep: Reputation: 83
what language or script language???

in bash script language, you can try something like this:
Code:
#!/bin/bash
a=12
if [[ "$a" -eq 12 ]]
then
  echo "is equal"
else
  echo "is not equal"
fi

Last edited by 16pide; 08-11-2011 at 04:20 AM.
 
Old 08-11-2011, 04:29 AM   #3
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thanks , maybe you don't understand me.
I am using linux bash.
Code:
a=var
$a=value !!!error happens!!!
I want the following result:
Code:
$echo $a
var
$echo $var
value
So I want the value of $a to be a variable, and it can be valued.

Last edited by 915086731; 08-11-2011 at 04:32 AM.
 
Old 08-11-2011, 05:35 AM   #4
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,275

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
There is no such construct in bash as I known, you may use "eval" to achieve this behavior:
Code:
a=var
eval $a=value
 
Old 08-11-2011, 06:03 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027
I'd like to know what your real purpose is, because this has all the hallmarks of an XyProblem.

You want to evaluate a variable, so that you can use the value as the name of another variable. This can't generally be done directly. It can be done with eval, as above, but the use of eval is risky, not recommended, and probably not necessary.

Chances are that what you want to do can better be done with an array, or an indirect reference, or something similar, but we'd need to hear the details first.

At the very least, the above example can be done reasonably more safely using declare instead of eval.
Code:
a=var
declare $a=value

Last edited by David the H.; 08-11-2011 at 06:05 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
/ disk full evaluate? EDDY1 Linux - Newbie 2 08-08-2011 08:04 PM
Somebody evaluate my fstab, Can't hibernate shadogamon Linux - Software 2 07-24-2011 11:31 PM
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 12:31 PM
evaluate size of output command slugman Linux - General 3 11-19-2010 01:07 AM
Basic Bash: How to use eval to evaluate variable names made of arbitrary strings. GrapefruiTgirl Programming 9 12-16-2009 11:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration