I spent a chunk of time this morning engaged in what ended up being something of a red herring, but learning was involved along the way, so here it is… how to set an environment variable indirectly in a bash shell.
Suppose I have a variable TAG=key and a variable VARVAL=thisval.
#Set key_val=$VARVAL eval ${TAG}_val=\$VARVAL #Export key_val=$VARVAL export eval ${TAG}_val=\$VARVAL
Now suppose I want to test if $TAG exists, and further whether it is set to the same value as $CURRTAG. The ${TAG:+1} tests whether that TAG variable exists and that it is not empty. The -a is a logical AND.
if [ -n "${TAG:+1}" -a "$TAG" != "$CURRTAG" ]; then tmpf=${TAG}_val export VARVAL=${!tmpf} export CURRTAG=$TAG fi
Erm, I think… I realised this wouldn’t actually be appropriate for the context I had in mind so never fully tested it…