Seems that the recommended way of doing indirect variable setting in bash is to use eval
:
var=x; val=foo
eval $var=$val
echo $x # --> foo
The problem is the usual one with eval
:
var=x; val=1$'
'pwd
eval $var=$val # bad output here
(and since it is recommended in many places, I wonder just how many scripts are vulnerable because of this...)
In any case, the obvious solution of using (escaped) quotes doesn't really work:
var=x; val=1"$'
'pwd"
eval $var="$val" # fail with the above
The thing is that bash has indirect variable reference baked in (with ${!foo}
), but I don't see any such way to do indirect assignment -- is there any sane way to do this?
For the record, I did find a solution, but this is not something that I'd consider "sane"...:
eval "$var='"${val//'/'"'"'}"'"
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…