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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
11-29-2005, 10:27 AM
#1
Member
Registered: Oct 2005
Location: Germany
Distribution: Ubuntu
Posts: 51
Rep:
bash parameter indirection possible?
I want to get the value of a parameter whose name (unknown) is in another parameter (known).
$ n=2
$ set aaa bbb
$ p="$"$n
$ echo $n $2 $p
2 bbb $2
$p contains the name $2 whose value "bbb" I want to get.
Assume we know only the name of $p.
$ echo $(echo $p)
$2
###### (expect bbb, not $2)
$ pp=$(echo $p)
$ echo $pp
$2
####### (ditto)
####### (try an extra indirection)
$ pp=$(echo $(echo $p))
$ echo $pp
$2
A) can someone explain why it does not work?
B) how can I do what I want to do?
11-29-2005, 10:43 AM
#2
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
eval
Code:
billym.>a=hello
billym.>b=a
billym.>eval c=\$$b
billym.>echo $c
hello
11-29-2005, 11:07 AM
#3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524
Rep:
(assuming bash)
Code:
#!/bin/bash
a=hello
b=a
echo ${!b}
Or, for kornelix' example:
Code:
#!/bin/bash
n=2
set aaa bbb
p=${!n}
echo $n $2 $p
11-30-2005, 03:28 AM
#4
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
doesn't work on my ksh Hko
and you have
#!/bin/bash at the top
SunOS primadtpdev 5.8 Generic_108528-20 sun4u sparc SUNW,Ultra-250
11-30-2005, 07:52 AM
#5
Member
Registered: Oct 2005
Location: Germany
Distribution: Ubuntu
Posts: 51
Original Poster
Rep:
thanks billy and Hko. works fine.
I understand the eval, I think
(interpret (interpret xxxxx))
I need to dig on the use of {!string}.
Can't find it in man bash.
11-30-2005, 08:35 AM
#6
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
eval will work on sh, bash, ksh,
it's best to not get too bash-specific if you work in
the outside world.
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 03:04 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News