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
  • 69,223
  • 56
  • 216
  • 327
0x1337
  • 81
  • 1
  • 1
  • 2
  • Do you have other variables in /etc/environment, and do they show up in the `printenv` output? – wjandrea Dec 28 '17 at 23:50
  • No, except the PATH maybe but not more. But when i'm doing printenv using the cmd Windows i have some variable like: TERM,NAME,HOME and some more, but none of them have been added by me. – 0x1337 Dec 29 '17 at 00:05

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
  • 100,877
  • 19
  • 193
  • 283
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'

Buddacow
  • 41
  • 5
0

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

Vladimir S.
  • 740
  • 7
  • 12
  • So "yourlinuxcommand" would be `/etc/environment`? I'm not so sure this would be the best place. I think Pather's answer is better... – WinEunuuchs2Unix Aug 31 '18 at 11:44
  • @WinEunuuchs2Unix `/etc/environment` is not command but it is a system-wide configuration file! You can set your user variables in 3 files: `/etc/environment`, .profile or .bashrc ... to use them, you have to run the command in my answer! So the command must be, for example, like: `bash --login -c "printenv"` – Vladimir S. Aug 31 '18 at 11:59
  • I see your answer has changed. Disregard my first comment. – WinEunuuchs2Unix Aug 31 '18 at 12:21
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.

WinEunuuchs2Unix
  • 99,709
  • 34
  • 237
  • 401