DEV Community

Discussion on: Tricks of declaring dynamic variables in Bash

Collapse
 
moopet profile image
Ben Sinclair

Given the format you've chosen for storing variables is the same as regular variable assignment in bash, could you explain the technical benefits of doing this beyond sourcing the configuration file with . ./variables.txt ?

I see a couple of practical benefits:

  • you don't have to stick to that format
  • you can handle validation in your import loop
Collapse
 
a1ex profile image
Cheng Pan

several benefits I can think of now:

  • file sometimes might not be a desired format, eg when dealing with secrets in CI environment, you want the secrets live in memory instead of stored in files.
  • sometimes you might be reading the variable name and value from external sources, eg. Vault. And the set of key/values are unknown ahead of time. In that case, you are not dealing with files directly, even though you can still use it as indirection.
  • in my case when I discovered the tricks, I wanted to declare the variables dynamically and export them at the same time. If we source the variables from variables.txt, you still need to export the variables separately