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.
add_prefix () {
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.
Thanks. I appreciate your help.
Regards
Hon-Chi