[mdlug] Evil eval

Jeff Hanson jhansonxi at gmail.com
Wed Oct 1 18:47:54 EDT 2014


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


More information about the mdlug mailing list