LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-23-2012, 10:18 AM   #1
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Rep: Reputation: 15
Bash - assign a value


Hi,

I'd like to do something like that
Code:
#!/bin/bash
hello=varname
$hello=kennedy

echo $varname
I expected the output to be "kennedy".
However it just won't work

Any thoughts?

BlueSpirit

Last edited by BlueSpirit; 01-23-2012 at 10:21 AM.
 
Old 01-23-2012, 10:23 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 241Reputation: 241Reputation: 241
Although powerfull, bash is just shell language

I mean for whatever variable value it sees at the start of line, it will try to execute it as a command

The indirect reference works like this
Code:
#!/bin/bash
varname=hello
hello=kennedy

echo ${!varname}

Last edited by Cedrik; 01-23-2012 at 10:37 AM.
 
Old 01-23-2012, 12:15 PM   #3
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 135

Rep: Reputation: 29
You can do something like this

Code:
#!/bin/bash
hello=varname
eval $hello=kennedy
echo $varname
then in the second line the $hello is expanded by bash so that you are actually evaluating "varname=kennedy"
 
Old 01-23-2012, 12:39 PM   #4
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,334

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Please don't use eval for things like this. It's a very insecure option.

There are much better ways to do this. Have a look here:

http://mywiki.wooledge.org/BashFAQ/006

I'd give good odds that an associative array, or possibly even a regular array, would be a better solution than an indirect reference, although we'd need more details about your actual goals to tell for sure.
 
Old 01-24-2012, 11:34 PM   #5
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 400

Rep: Reputation: 41
Lightbulb

David ->

Thanks for sharing this knowledge-base:

http://mywiki.wooledge.org/BashFAQ/006

I have now got that "eval" is a really a misspelling for "evil" and would look for better approaches/substitutions of it.
 
Old 01-25-2012, 05:56 AM   #6
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,334

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Glad to be of service.

To be fair, there are legitimate uses for eval. You just have to be very cautious about what you are doing, and only use it when you are very sure about what's happening every step of the way.

The main point I want to convey is that there are nearly always other, safer, better options available for doing this kind of thing.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] (BASH) How to assign 2 variables to same line? Dick Dastardly Programming 10 01-15-2012 09:17 AM
Bash scripting...need to read a value from a file then assign to variable trekgirl Programming 9 02-27-2011 11:42 PM
[SOLVED] [bash] assign boolean expression to variable hashbang#! Programming 10 08-16-2009 08:44 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 06:29 PM
bash script Want to capture return key and assign a value procfs Programming 9 07-07-2006 01:38 AM


All times are GMT -5. The time now is 02:59 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration