WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Provide a way to set environment variables when launching bash

Open xilun opened this issue 10 years ago • 8 comments

There should be a way to pass some environment variables from the Win32 bash.exe launcher to the launched bash WSL process. One use case would be for example to set BASH_ENV to custom values for each non interactive invocation of bash. Of course there are plenty of other useful environment variables depending on the tool being targeted, so the most useful would be a generic way to specify which variable to pass and/or to override.

Because the Windows PATH, for instance, has nothing to do with the WSL PATH, this cannot just be a list of variables to copy from the Windows environment using the same name. Full new name=values would have to be specified on the command line with a special option. Another idea would be a special prefix that would be stripped, for example Windows environments variables _WSL_NAME=VALUE would become NAME=VALUE under WSL.

xilun avatar Apr 17 '16 02:04 xilun

It's possible. I made instruction how to do this here: #24

You can make this if you want set additional variables (run this in cmd.exe):

bash -c "bash -c 'source /LINUX_PATH_TO_FILE_WITH_VARIABLES'"

Or to load default bash variables just simply type:

bash -c "bash"

You can make .bat file as a shortcut:

echo bash -c "bash" > bash_with_default_variables.bat

or

echo bash -c "bash -c 'source /LINUX_PATH_TO_FILE_WITH_VARIABLES' > bash_with_custom_variables.bat

patryk9200 avatar Apr 18 '16 10:04 patryk9200

If you're looking for additional support, I suggest adding something over to our UserVoice page.

russalex avatar Apr 18 '16 23:04 russalex

Additionally I'd suggesting adding any additional environment variables you need to you .bashrc file.

benhillis avatar Apr 19 '16 00:04 benhillis

Well it was more about dynamically assigning environment variables without creating extra processes or files.

If creating a new file is tolerated, I also found a solution with --rcfile for interactive shells.

xilun avatar Apr 19 '16 09:04 xilun

I guess the existing solutions are good enough.

xilun avatar Apr 19 '16 09:04 xilun

With WSL on Windows 10 Creators Update you can use this command: C:\Windows\System32\bash.exe ~ --login and then edit .profile in your home directory.

hapihakr avatar Apr 17 '17 02:04 hapihakr

@benhillis Your suggestion confuses system and per-user environments. This isn't great when trying to replicate server environments where multiple processes might run as different users but have the same PATH set by /etc/environment. This is Ubuntu 101 stuff, not sure how WSL can claim to be "Ubuntu" but not support system environment: https://help.ubuntu.com/community/EnvironmentVariables#System-wide_environment_variables

thesmart avatar Dec 07 '17 19:12 thesmart

@hapihakr Where's the --login argument documented? The --help doesn't say anything about what that argument does and it's not doing anything extra from what I can tell. My .profile is unchanged and I have no Windows environment variables in bash.

sw-carlin avatar Dec 07 '17 19:12 sw-carlin