1

How to echo a dynamic variable's content in shell script ?

i=1
declare  x$i=help
echo  $(echo x$i)
echo $x$i

Current output:

x1  
1

Desired output:

help
Jahid
  • 21,542
  • 10
  • 90
  • 108

2 Answers2

0

Use eval:

i=1
declare  x$i=help
eval "echo \$x$i"
user464502
  • 2,203
  • 11
  • 14
0

This should work:

echo  $(tmpvar=x$i && echo ${!tmpvar})

Example:

i=1
declare  x$i=help
echo  "$(tmpvar=x$i && echo ${!tmpvar})"

Output:

help
Jahid
  • 21,542
  • 10
  • 90
  • 108