The docs say that I can access environment variables like this:
database_name = "${?DB_NAME}"
Where DB_NAME
is an environment variable. Do I have to put the question mark ?
or is that there to prevent a crash if it doesn't exist?
Also, are we allowed to reference a variable in a file before it is declared?
foo = "hello-${bar}"
bar = "baz"
Environment variables help programs know what directory to install files in, where to store temporary files, and where to find user profile settings. They help shape the environment that the programs on your computer use to run.
The command env displays all environment variables and their values. The command printenv can also be used to print a single variable by giving that variable name as the sole argument to the command.
There are multiple solutions: you ask each developer to set the value in their environment before launching the application. you add some logic at the application's initialization to use the API key environment variable value if it exists, otherwise, fall back to the plain configuration file.
env files. In some situations you only need an environment variable set for only a single project. In that case . env files are a great solution.
1- ?
means optional. If the environment variable does not exist at runtime, the whole line will be ignored, as if it didn't exists in the first place. So use it when you want optional overriding.
See Optional Env Vars section of the docs for more info.
Note that ?
works for all substitutions and not just env vars.
2- Yes, you can reference a variable which is defined later in the config file (forward referencing). As long as the value exists and there is no cycle (circular dependency), the substitution will be done successfully.
Think of it like this: first the whole file will be parsed at runtime and the value of the literals will be bound to their vars, and then the substitutions will take place, so the order doesn't really matter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With