0

This question already has an answer here:

Say for example...

#!/bin/bash
a1=one a2=two a3=three
for x in {1..3}; { echo -n "\$a$x "; }

Actual..

$a1 $a2 $a3

Expected..

one two three

How can one echo, reference, call, or what have you... a variable - by whatever incantation of it's name you deem fit? Basically, dereference a variable by its "string" "name".

I've attempted numerous/various/logical things involving single/double quotes, parenthesis, brackets, echo, eval, and exec, without luck.

This is a generalized, more broad version of Shell, Concatenating 2 strings to reference a 3rd variable, which I feel could benefit those trying to simply reference a variable, not necessarily by concatenation, etc.

marked as duplicate by don_crissti, PSkocik, cuonglm, Scott, slm Sep 26 '15 at 2:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @don_crissti Huh, what do ya know.. that works! Can you post it as an answer? Maybe explain what that explanation mark is doing in there/how it's working it's magic? – alex gray Sep 26 '15 at 0:59
  • @don_crissti From your tip, I figured out I could also do for x in a{1..3}; { echo ${!x}; }. Neat. – alex gray Sep 26 '15 at 1:03