title: Indirect variable assignment in bash
body: <p>Seems that the recommended way of doing indirect variable setting in bash is to use <code>eval</code>:</p>
  
  <pre><code>var=x; val=foo
  eval $var=$val
  echo $x  # --&gt; foo
  </code></pre>
  
  <p>The problem is the usual one with <code>eval</code>:</p>
  
  <pre><code>var=x; val=1$'\n'pwd
  eval $var=$val  # bad output here
  </code></pre>
  
  <p>(and since it is recommended in many places, I wonder just how many scripts are vulnerable because of this...)</p>
  
  <p>In any case, the obvious solution of using (escaped) quotes doesn't really work:</p>
  
  <pre><code>var=x; val=1\"$'\n'pwd\"
  eval $var=\"$val\"  # fail with the above
  </code></pre>
  
  <p>The thing is that bash has indirect variable reference baked in (with <code>${!foo}</code>), but I don't see any such way to do indirect assignment -- is there any sane way to do this?</p>
  
  <p>For the record, I did find a solution, but this is not something that I'd consider "sane"...:</p>
  
  <pre><code>eval "$var='"${val//\'/\'\"\'\"\'}"'"
  </code></pre>