8

I'm using WSL and I'm quite new on Linux. I have set an environment variable in /etc/environment called TESTVAR. This variable is required for some projects and it's working just fine when I'm running Bash directly.

But if I'm running a Linux command using the Windows cmd like: bash.exe -c "printenv", the variable doesn't exist. So I can't run my commands using cmd otherwise I'll get many errors because the variable contains an important path.

What do I need to do?

Thanks.

Zanna
  • 72,312
0x1337
  • 81

4 Answers4

5

There are several methods for bash on Windows

See https://github.com/Microsoft/WSL/issues/24

Basically you add them to ~/.bashrc

If that is not working post your .bashrc

I don't know why /etc/environment is not working.

Panther
  • 104,528
1

It seems that if you want to use your Linux (Ubuntu) environment variables, you have to run the following command:

bash --login -c "printenv"

You can see some discussion relating to the issue in this link

1

Old post but if anyone stumbles here from google...

As of Windows version 17134, adding the -i flag to the bash command will run the shell command as "interactive" which will invoke the full linux environment including any login dot files that are set up (.bashrc, .bash_aliases, etc.) which might hold environmental or variable definitions.

For the original question, from Windows cmd this should have the desired effect: bash -ic printenv

See also: bash -ic 'man bash'

0

WSL Bash works the same as Ubuntu Bash

EDIT: Original answer below for historical comments. Panther's answer is best. Your ~/.bashrc file which is processed each time the terminal is opened. In it place the command:

MY_VARIABLE="some text"

Then you can use echo $MY_VARIABLE to see it's setting.


I opened a new Bash Terminal Window in WSL which works the same way in Ubuntu 16.04 except the splash screen is subtlety different:

enter image description here

As you can see once you set an environment variable it can be recalled normally.

Perhaps your bigger problem is with your command:

bash.exe -c "printenv"

On one of my Windows 10 desktop shortcuts I use:

C:\Windows\System32\bash.exe -c "cd && DISPLAY=0:0 /mnt/e/bin/lock-screen-timer"

Perhaps you are missing something there???


Reply to comments

In response to your comments to someone else, my system, like yours, reveals:

rick@alien:/mnt/c/Windows/System32$ echo $TERM,$NAME,$HOME
xterm-256color,alien,/home/rick

So you should not be unduly concerned.