PowerShell: Setting an environment variable for a single command only

前端 未结 8 907
闹比i
闹比i 2020-11-29 02:17

On Linux, I can do:

$ FOO=BAR ./myscript

to call \"myscript\" with the environment variable FOO being set.

Is something similar pos

8条回答
  •  青春惊慌失措
    2020-11-29 03:17

    I got motivated enough about this problem that I went ahead and wrote a script for it: with-env.ps1

    Usage:

    with-env.ps1 FOO=foo BAR=bar your command here
    
    # Supports dot-env files as well
    with-env.ps1 .\.env OTHER_ENV=env command here
    

    On the other hand, if you install Gow you can use env.exe which might be a little more robust than the quick script I wrote above.

    Usage:

    env.exe FOO=foo BAR=bar your command here
    
    # To use it with dot-env files
    env.exe $(cat .env | grep.exe -v '^#') SOME_OTHER_ENV=val your command
    

提交回复
热议问题