0

I want to retrieve specific variable values depending on 'prod' or 'dev' environment, without chaining lots of if-else blocks with the same content.
Therefore, I have the variables like this:

prod_key=daksjhdkjasd
dev_key=kajsdnaskldk

I want to evaluate the correct key in some way like this:

env=prod

key=${env}_key

Of course I've tried the above and it didn't work, just replaced the 'env' variable. (I'm using dash (/bin/sh))

Teodoro
  • 1,194
  • 8
  • 22
  • You are interested in indirection (See [BashFAQ 006](https://mywiki.wooledge.org/BashFAQ/006#Indirection)). Indirection is not part of POSIX but can be _mimicked_ using `eval` with all its consequences. – kvantour Aug 06 '20 at 15:27
  • 1
    The link you provided a minute ago had the answer! I did this `eval \"\${${env}_key}\"` and it worked.. Ugly, but it works. I'm not concerned about security here either. Thank you! – Teodoro Aug 06 '20 at 15:35

0 Answers0