The beauty of the environment is that it can be manipulated prior to running the program. I don't understand the desire to have runtime .env loaders when that env can just be established before the program is run. Every language can read env without a library. The churn in this space is too high... it's a smell.
I have a shell function for this in my zsh config:
function lenv() {
export $(cat .env | xargs)
}
It doesn't get any more unix than this. I do not allow my team to build software that autoloads environment files using libraries. It's such a mess. That is a concern of the shell.
You're not wrong, but there's a small detail that your shell function doesn't solve for: unloading the environment variable once it's no longer needed.
If you maintain one shell per process-that-needs-a-secret-in-the-env then it doesn't matter, but if you're using long-running shells then direnv can load and unload env vars. It also gives you a nice little heads-up that it loaded/unloaded variables.
Sort of? It definitely does what's needed, but it also removes any additional environment variables that were set, unless they're in your shell initialisation files.
I'm not trying to persuade that these tools are essential or anything, just highlighting that there's a few small details some take care of that're fairly nice.
Comments
The beauty of the environment is that it can be manipulated prior to running the program. I don't understand the desire to have runtime .env loaders when that env can just be established before the program is run. Every language can read env without a library. The churn in this space is too high... it's a smell.
I have a shell function for this in my zsh config:
It doesn't get any more unix than this. I do not allow my team to build software that autoloads environment files using libraries. It's such a mess. That is a concern of the shell.You're not wrong, but there's a small detail that your shell function doesn't solve for: unloading the environment variable once it's no longer needed.
If you maintain one shell per process-that-needs-a-secret-in-the-env then it doesn't matter, but if you're using long-running shells then direnv can load and unload env vars. It also gives you a nice little heads-up that it loaded/unloaded variables.
Use this wrapper instead:
or just `exec $SHELL` to unload.
Sort of? It definitely does what's needed, but it also removes any additional environment variables that were set, unless they're in your shell initialisation files.
I'm not trying to persuade that these tools are essential or anything, just highlighting that there's a few small details some take care of that're fairly nice.
Word splitting works with whitespace in general. You don't need the `xargs`.