0

Suppose I have the following 2 variables:

color=black
round_black_car=IGK47546

Is it possible to print IGK47546 with the variable color?

echo ${round_${color}_car} doesn't work. It gives me bad substitution error.

Brian
  • 12,145
  • 20
  • 90
  • 153
  • See [BashFAQ #6](http://mywiki.wooledge.org/BashFAQ/006#Evaluating_indirect.2Freference_variables) for a best-practices approach. (`eval` is **absolutely not** a best-practices approach; [BashFAQ #48](http://mywiki.wooledge.org/BashFAQ/048) goes into reasons why it's best avoided). – Charles Duffy Jun 23 '17 at 04:05

1 Answers1

-1

You should use eval expression combine with backslash to perform the replacement.

$ color=black
$ round_black_car=IGK47546
$ eval echo \${round_${color}_car}
IGK47546
alijandro
  • 11,627
  • 2
  • 58
  • 74
  • 1
    This is not a safe practice, as it parses data as code -- a key component in injection attacks, privilege escalation attacks (when one can provide data used by scripts run with different permissions), and other kinds of security vulnerabilities. Consider the case of `color='}$(rm -rf ~)${'` as an illustrative example. – Charles Duffy Jun 23 '17 at 03:22