LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-29-2005, 11:27 AM   #1
kornelix
Member
 
Registered: Oct 2005
Location: Germany
Distribution: Ubuntu
Posts: 57

Rep: Reputation: 24
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?
 
Old 11-29-2005, 11:43 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
eval

Code:
billym.>a=hello
billym.>b=a
billym.>eval c=\$$b
billym.>echo $c
hello
 
Old 11-29-2005, 12:07 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
(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
 
Old 11-30-2005, 04:28 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
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
 
Old 11-30-2005, 08:52 AM   #5
kornelix
Member
 
Registered: Oct 2005
Location: Germany
Distribution: Ubuntu
Posts: 57

Original Poster
Rep: Reputation: 24
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.
 
Old 11-30-2005, 09:35 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
eval will work on sh, bash, ksh,

it's best to not get too bash-specific if you work in
the outside world.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
setting up scp alias under bash - parameter passing problem gjr Linux - Newbie 4 06-08-2005 07:38 AM
c++: ERROR: Invalid indirection ( post 2) ashirazi Programming 2 08-26-2004 11:15 AM
c++: ERROR: Invalid indirection ashirazi Programming 0 08-22-2004 01:09 PM
bash: pass a parameter to xview nimra Programming 2 06-28-2004 10:18 PM
Bash function parameter Misel Programming 2 05-17-2003 12:51 PM


All times are GMT -5. The time now is 09:46 AM.

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 Google+: linuxquestions
Open Source Consulting | Domain Registration