## Better Way to Get Value of Value of Variable in Bourne shell?

### Better Way to Get Value of Value of Variable in Bourne shell?

Hi

I need to create a Bourne shell function that can change/manipulate the values
of the given variable names, e.g.

\$ P1=John
\$ P2=Jane
\$ P3=Paul

\$ echo "1=\$P1, 2=\$P2, 3=\$P3."
1=John, 2=Jane, 3=Paul.

\$ add_prefix Hi_ P1 P2 P3

\$ echo "1=\$P1, 2=\$P2, 3=\$P3."
1=Hi_John, 2=Hi_Jane, 3=Hi_Paul.

After several trial-and-error, I finally got the add_prefix() working, but I
think my solution is too complicated and inefficient, specifically the
"eval \$var='\$prefix`eval echo \\\$\$var`'" that takes 2 evals and 1 command
substitution, as shown below.

prefix=\$1
shift
for var in \$*; do
eval \$var='\$prefix`eval echo \\\$\$var`'
done
}

Can any Bourne shell programming guru show me a more concise, efficient and
elegant way in writing the add_prefix() function without using any external
command?

Is there any shorter syntax like \${\$var} in Bourne shell that can replace
`eval echo \\\$\$var` ?  Something concise and simpler will be helpful because
the actual function I need to write is much more complicated than add_prefix()
function shown above.

Regards
Hon-Chi

### Better Way to Get Value of Value of Variable in Bourne shell?

Since you don't have arrays in Bourne (you do in ksh, btw), your
solution is not a bad approach.
If it were me, I would simplify things a bit:
P1="Hi_\$P1"
P2="Hi_\$P2"
P3="Hi_\$P3"
etc.
But I don't know the specifics of your application, so I don't know what
you're trying to do.

Might I suggest using the arrays in ksh?

> Hi

> I need to create a Bourne shell function that can change/manipulate the values
> of the given variable names, e.g.

>   \$ P1=John
>   \$ P2=Jane
>   \$ P3=Paul

>   \$ echo "1=\$P1, 2=\$P2, 3=\$P3."
>   1=John, 2=Jane, 3=Paul.

>   \$ add_prefix Hi_ P1 P2 P3

>   \$ echo "1=\$P1, 2=\$P2, 3=\$P3."
>   1=Hi_John, 2=Hi_Jane, 3=Hi_Paul.

> After several trial-and-error, I finally got the add_prefix() working, but I
> think my solution is too complicated and inefficient, specifically the
> "eval \$var='\$prefix`eval echo \\\$\$var`'" that takes 2 evals and 1 command
> substitution, as shown below.

>     prefix=\$1
>     shift
>     for var in \$*; do
>       eval \$var='\$prefix`eval echo \\\$\$var`'
>     done
>   }

> Can any Bourne shell programming guru show me a more concise, efficient and
> elegant way in writing the add_prefix() function without using any external
> command?

> Is there any shorter syntax like \${\$var} in Bourne shell that can replace
> `eval echo \\\$\$var` ?  Something concise and simpler will be helpful because
> the actual function I need to write is much more complicated than add_prefix()
> function shown above.

--
+---------------------+---------------+

+---------------------+---------------+

### Better Way to Get Value of Value of Variable in Bourne shell?

> Since you don't have arrays in Bourne (you do in ksh, btw), your
> solution is not a bad approach.
> If it were me, I would simplify things a bit:
> P1="Hi_\$P1"
> P2="Hi_\$P2"
> P3="Hi_\$P3"
> etc.

If I can hard-code them, I won't bother trying to write a function to do the
job.  :^)  The actual manipulation of variables I need isn't just simple as
prefixing words.

Quote:

> But I don't know the specifics of your application, so I don't know what
> you're trying to do.

To have a function defined in user's interactive Unix shell (sh, ksh & bash)
that can conditionally manipulate the values of given variables, when invoked
by user.

Quote:

> Might I suggest using the arrays in ksh?

Unfortunately, no, because
a) the function needs to be in Bourne sh since it is the least common
denominator among sh, ksh & bash.
b) the function is to be used interactively and manipulate variables in
current shell, hence can't be programmed as external shell script.

Hon-Chi

### Better Way to Get Value of Value of Variable in Bourne shell?

>     prefix=\$1
>     shift
>     for var in \$*; do
>       eval \$var='\$prefix`eval echo \\\$\$var`'
>     done
>   }

The echo, command substitution and second eval are unnecessary
(and potentially dangerous with special characters).
All you need is

eval "\$var=\$prefix\\$\$var"

Note double quotes.

--
Tapani Tarvainen

### Better Way to Get Value of Value of Variable in Bourne shell?

Ah well, just wondering.
My advice is to stick with what works (ie, what you have).

Best of luck!
Dave

> > Might I suggest using the arrays in ksh?

> Unfortunately, no, because
> a) the function needs to be in Bourne sh since it is the least common
>    denominator among sh, ksh & bash.
> b) the function is to be used interactively and manipulate variables in
>    current shell, hence can't be programmed as external shell script.

--
+---------------------+---------------+

+---------------------+---------------+

### Better Way to Get Value of Value of Variable in Bourne shell?

> The echo, command substitution and second eval are unnecessary
> (and potentially dangerous with special characters).
> All you need is

>        eval "\$var=\$prefix\\$\$var"

> Note double quotes.

Thanks.  This is the improvement over my original code that I was looking for.

Hon-Chi

Disclaimer: BTW, for those of you who are curious, besides asking a somewhat
similar question as mine on the same day and also using Google Groups, the

each other, we aren't taking same class, nor attending same school, nor being
assigned same homework/quiz, nor we work for the same company.  :^)

Hi Barry,

what is the advantage of using "wc -l < filename" over
"wc -l filename"? I see this all the time in this newsgroup.

--

Regards,

Bruce

+--------------------------------------------------------------------------+
| Bruce Fountain                 NEC Australia - New Product Development   |

+--------------------------------------------------------------------------+