dotenv-rs / dotenv

Library to help supply environment variables in testing and development
MIT License
557 stars 85 forks source link

Using .env variables inside shell #66

Closed CallistoM closed 1 year ago

CallistoM commented 3 years ago

Is there any possible way to use .env variables inside shell? .env file:

export REDIS_ADDRESS=localhost:6379 MEANING_OF_LIFE=4

For example using Windows: $env:MEANING_OF_LIFE after using binary and does it work cross platform?

daboross commented 3 years ago

IIRC there isn't a cross-platform way for programs to affect the environment of the shell they were run in - and this isn't really possible in the general case, either.

I'm not sure about windows, but in Linux you can either have your program start a shell (and thus have that shell inherit the env variables from the program) or output text (like export REDIS_ADDRESS=localhost:6379) from stdout, and then have the user manually execute that (for instance, eval $(my-program-binary). It isn't possible to directly modify the outer shell's environment, so most programs which need to alter the shell do one of these two things.

This might be an XY problem, though. What are you specifically trying to do?

CallistoM commented 3 years ago

@daboross Thanks for replying. I'm trying to accomplish a cross-platform way of adding to the path variable so various binaries can be called on shell for example: clang.exe on Windows and clang binary on mac for example. dotenv would suffice since the .env file is variable but I saw from your comment that this is not possible.

Running this script from a .bat file results in a possibility to use the binary: set LOCAL_CLANG_DIR=%~dp0clang\ set "PATH=%LOCAL_CLANG_DIR%;%PATH%"

And running export from Mac or Linux on shell for example results in a possibility to use a binary. I would like to translate this functionality with dotenv to a working binary for various platforms.

daboross commented 3 years ago

That makes sense! I can see what you're trying to do. I guess all I can say is - good luck! :sweat_smile:

I know you can export in unix shells, but even then you have to run that in the shell, either directly or with a sourced .sh file. There's no way to just run a .sh and have the environment change. You need to hook into the shell itself, or have the user do that through using export (or otherwise executing shell commands that you print to stdout).

If you do get it working, I'd be very interested to see it! I don't think it's within the scope of the dotenv library, but if you get it working and in a nice-to-use way that would be pretty neat.

daboross commented 3 years ago

After re-reading your comment, I think I have one other possible piece of advice:

If you only need to execute a limited set of binaries with the environment variables set, that should be as easy as running the commands using std::process::Command after loading dotenv::env. Spawned processes always inherit the environment from their parents, so if you know what commands you need to run, then it should be fairly easy to just run those from within your rust binary.