[mdlug] Evil eval

Carl T. Miller carl at carltm.com
Thu Oct 2 06:28:37 EDT 2014


Jeff Hanson wrote:
> An eval echo statement is not working as I would expect.  The first
> derived
> variable name works with the suffix quoted or escaped.  The second only
> works quoted.  Since it behaves the same in both Bash and Dash I'm
> assuming
> it's my fault.  Example with various combinations in the output:
>
> #! /bin/sh
> test1a_uc="A"
> test1b_uc="B"
> test1c_uc="C"
>
> for varname in test1a test1b test1c; do
>     eval $varname"_lc"=\$\(echo "\$$varname""_uc | tr '[:upper:]'
> '[:lower:]'"\)
> done
>
> # Output
> echo "Quotes both:"
> for varname in test1a test1b test1c; do
>     eval echo "$varname""_lc: \$$varname""_lc"
> done
> echo "Escape first, quotes second:"
> for varname in test1a test1b test1c; do
>     eval echo "$varname\_lc: \$$varname""_lc"
> done
> echo "Quotes first, escapes second:"
> for varname in test1a test1b test1c; do
>     eval echo "$varname""_lc: \$$varname\_lc"
> done
> echo "Escapes both:"
> for varname in test1a test1b test1c; do
>     eval echo "$varname\_lc: \$$varname\_lc"
> done

Jeff, try using this line where you want to escape the
second derived variable.  I think this does what you
want it to.

eval echo "${varname}_lc: \$${varname}_lc"

c




More information about the mdlug mailing list