Variable substitution (what do you call / call this method)?

Tell bash I have:

c=3

b3=4

      

instead:

echo $b3 

      

what is the command to output "4"? I tried at the bottom but couldn't.

  • echo $b($(c))

  • echo $b{!c}

  • echo $b${c}

  • echo ${d[$c]}

+3


source to share


1 answer


You can use an indirect reference for this:

$ c=3
$ b3=4
$ d=b$c
$ echo "${!d}"
4

      



Although, in general, arrays should be used instead of indirect references whenever possible.

+6


source







All Articles