Reputation: 648
I have following scenario.
#!/bin/bash
export avalue_name="stack"
LOOPERS=${LOOPERS:-avalue}
for LOOPER in $(echo "$LOOPERS" | tr ',' "\n")
do
actualVar="$LOOPER""_name"
echo "actualVar - $actualVar"
done
Output is
actualVar - avalue_name
I want the output to be
actualVar - stack
How would I do it?
I tried $($LOOPER_name)
. It doesn't work either.
Upvotes: 2
Views: 216
Reputation: 42137
Two approaches:
Use an intermediate variable, and use shell indirect reference on that:
$ avalue_name="stack"
$ LOOPER=avalue
$ temp="${LOOPER}_name"
$ echo "${!temp}"
stack
Or use eval
, not recommended:
$ eval echo "\$${LOOPER}_name"
stack
Points:
_
is a valid variable constituent character, so while referring a variable you need to enclose it with {}
when another valid variable constituent character is immediately before or after it
unless absolutely necessary, do not use all uppercases for user-defined variables to prevent possible mix-up with the environment variables
Upvotes: 3