Operating System - HP-UX
1828581 Members
2500 Online
109982 Solutions
New Discussion

Posix Shell: shell variable evaluation

 
SOLVED
Go to solution
Peter Kain
Advisor

Posix Shell: shell variable evaluation

For lack of a better term, can you double evaluate an environment variable on a single
line in the Posix shell. Example ...

$ a=1
$ echo $a
1

$ b=a
$ echo $b
a

I want to evaluate b so that it returns 1.

Something like
echo $($b) --returns--> 1

This way a change to "a" is reflected when evaluating b. If we now change "a" to "a=2" then evaluating b will now return 2.

Please do not suggest "b=$a" that changes the problem.
Pleae no perl solutions.

Thanks,
Pete
9 REPLIES 9
Massimo Bianchi
Honored Contributor
Solution

Re: Posix Shell: shell variable evaluation

Hi,

escher:root/home/root# export a=1
escher:root/home/root# echo $a
1
escher:root/home/root# export b=a
escher:root/home/root# echo $b
a
escher:root/home/root# echo $$b
2767b
escher:root/home/root# eval echo \$$b
1



HTH,
Massimo

Jean-Louis Phelix
Honored Contributor

Re: Posix Shell: shell variable evaluation

Hi,

/tmp> a=b
/tmp> b=1
tmp> eval echo \$$a
1
/tmp>

Best regards ...
It works for me (© Bill McNAMARA ...)
Hai Nguyen_1
Honored Contributor

Re: Posix Shell: shell variable evaluation

Pete,

Try this:

# eval echo $`echo $b`

Hai
Bruno Vidal
Respected Contributor

Re: Posix Shell: shell variable evaluation

Hi,
another solution in order to avoid "\", and
that can be used directly in a script:
#a=1
#b=a
#echo $(($b))
1
#a=2
#echo $(($b))
2


cheers.

curt larson_1
Honored Contributor

Re: Posix Shell: shell variable evaluation

this is for the korn shell and probably applies to the posix shell also:

eval constructs a command by concatenating args(s) together, separating each with a space. ksh then reads and executes this command in the current environment. Note that command words are expanded twice; once to construct arg and again when ksh executes the constructed command.

an example

foo=10; x=foo
y='$'$x
print $y

$foo

eval y='$'$x
print $y

10
Peter Kain
Advisor

Re: Posix Shell: shell variable evaluation

Thanks everyone for the quick respone.


Not to take anything away from any of the answers but I liked Bruno's the best and was able to use incorporate the concept in my script.

Jean-Louis Phelix
Honored Contributor

Re: Posix Shell: shell variable evaluation

Hi,

Be careful ... It doesn't work !

#a=1
#b=a
#echo $(($b))
1
#a=2
#echo $(($b))
2
#a="it doesn't work"
#echo $(($b))
sh: it doesn't work: Syntax error
#eval echo \$$b
it doesn't work

Regards.

It works for me (© Bill McNAMARA ...)
Mike Stroyan
Honored Contributor

Re: Posix Shell: shell variable evaluation

/usr/dt/bin/dtksh can do this. It is based on ksh93. You can read the ksh93 manual with
man -M /usr/dt/man ksh

% typeset -n b=a
% a=1
% echo $b
1
% a=3
% echo $b
3
% b=5
% echo $a
5
% typeset +n b
% echo $b
a
Peter Kain
Advisor

Re: Posix Shell: shell variable evaluation

Jean-Louis,

I found out rigth away that you are correct.
As you probably realized, I had posted a simplified version of my issue.

I ended up using what you and Massimo posted.

eval echo \$$b

Once again thanks everyone.