LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-29-2008, 08:33 AM   #1
SwingingSimian
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Rep: Reputation: 0
BASH Using varaibles as variable names


Hi all


Is it possible to use a variable as a variable name, I see many documented instances where people mistakely do something like:

$foo=bar

WHen they really want to do:

foo=bar

I've also seen many people explain that this will try and set the value of foo as a variable name. I actually want to do this, e.g. set the value of a var called 'test'.

test=
foo=test
$foo=value_of_test

This results in:

bash: test=value_of_test: command not found


Is there a way around this? I've tried all sort of eval'ing, back ticks and dereferencing but I can't seem to get anything to work.

Thanks in advance

N
 
Old 08-29-2008, 08:37 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You are looking for indirect reference. I let the Advanced Bash Scripting Guide to explain this. See http://www.tldp.org/LDP/abs/html/ivr.html
 
Old 08-29-2008, 08:44 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

You must have been close if you used eval:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate indirect setting with bash, eval.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset

echo

test=
foo=test
$foo=value_of_test
echo " no   eval: foo is \"$foo\", test is \"$test\""

echo
eval $foo=value_of_test
echo " with eval: foo is \"$foo\", test is \"$test\""

exit 0
Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

./s1: line 14: test=value_of_test: command not found
 no   eval: foo is "test", test is ""

 with eval: foo is "test", test is "value_of_test"
More examples of eval at http://www.tldp.org/LDP/abs/html/internal.html#EX43 ... cheers, makyo
 
Old 08-29-2008, 01:28 PM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Is this what you were looking for?
Code:
#!/bin/bash
test=my_value
foo=$test
echo "foo="${!foo}

test=my_value_2
echo "foo="${!foo}
 
Old 08-29-2008, 01:46 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by gnashley View Post
Is this what you were looking for?
Code:
#!/bin/bash
test=my_value
foo=$test
echo "foo="${!foo}

test=my_value_2
echo "foo="${!foo}
Probably intended to write:
Code:
#!/bin/bash -

test=my_value
# foo=$test
foo=test
echo "foo="${!foo}

test=my_value_2
echo "foo="${!foo}
Producing:
Code:
foo=my_value
foo=my_value_2
cheers, makyo
 
Old 09-02-2008, 02:56 AM   #6
SwingingSimian
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by makyo View Post
Hi.

...

test=
foo=test
$foo=value_of_test
echo " no eval: foo is \"$foo\", test is \"$test\""

echo
eval $foo=value_of_test
echo " with eval: foo is \"$foo\", test is \"$test\""

...
... cheers, makyo
Thanks Mayko, this is exactly what I want, setting the value of the var via the 'aliased' variable name, rather than inheriting it via the alias.

Thanks very much for all your comments.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Perl Script: Variable Names composed of Variables wwnexc Programming 4 02-06-2006 03:47 AM
variable names in your code (fun) titanium_geek Programming 12 12-22-2003 10:12 AM
Variable names that include variables.... mychl Programming 7 09-24-2003 07:13 AM
Perl question re arrays/variable names Jon- Linux - Software 1 09-19-2003 10:29 AM

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

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