LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  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: 244Reputation: 244Reputation: 244
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: 151

Rep: Reputation: 34
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.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
Posts: 606

Rep: Reputation: 59
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.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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



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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:08 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