1

This question already has an answer here:

a=2
pt2=3
echo $a
2
echo $pt2
3

How can I use

echo $pt($a)

to print the contents of pt2?

An alternative is

b=pt$a
echo $b
pt2
echo $[$b]
3

marked as duplicate by Mikel, cuonglm, Anthon, slm, jasonwryan Jun 8 '14 at 7:32

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.

0
a=2
pt2=3
eval "echo \"\${pt$a}\""

3

Alternatively:

b=pt$a
eval "echo \"\${$b}\""

3

There are other methods:

a=2
pt2=3
export "pt$a=$((pt$a + $(echo pt$a)))"
echo $((pt$a))

6

export can work like eval in that way after a fashion with less hazards - but then there's the unexporting them to do later if you must. And $((arithmetic=assignment)) affects the current command in the current shell and therefore can be safer still for immediate variable assignment if the value is numeric.

There's also the shell's .dot and <<HEREDOCS for twice evaluating a variable assignment:

var1=var2 ; . <<HEREDOC /dev/stdin
    $var1=val
HEREDOC

echo $var2

val

Not the answer you're looking for? Browse other questions tagged or ask your own question.