1

I have a variable $var. I want to create another variable named test_$var.

How can I call this variable after the initialization?
echo $test_$var doesn't work.

Ciprian Vintea
  • 448
  • 2
  • 4
  • 18

1 Answers1

1

You can use declare shell bulletin:

var='abc'
var1="$test_$var"
declare $var1=5
echo "${!var1}"
5
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • The initialization works for me but I want to echo "$test_$var", not "$test_abc" – Ciprian Vintea Sep 15 '14 at 10:50
  • I ran into this situation lately myself and all the answers on this and related questions always seem to use declare. Is there a reason to prefer declare over eval? In my case I had to use eval since my shell is busybox and does not know declare. (for eval examples, have a look at [tldp](http://www.tldp.org/LDP/abs/html/ivr.html)) – a.peganz Sep 24 '14 at 10:34
  • `eval` is very dangerous unless you've 100% guarantee of controlling all the text that is used in `eval`. – anubhava Sep 24 '14 at 10:47