Set environment variable in Windows and WSL Linux in terminal
How to set an environment variable from a command line and shell
2 min readApr 21, 2020
Windows set env variable from the command line
- Open command line.
set API_KEY=123
echo %API_KEY%
should print yourAPI_KEY
.- This env variable is set for the context of the current cmd line.
Windows set env variable permanently using the command line
- Open the command line as admin.
setx API_KEY "123" /M
- Close the current shell. Open a new shell.
echo %API_KEY%
should print yourAPI_KEY
.- This env variable is set for all future shell instances permanently for your system.
WSL Linux set env variable from a bash terminal
- Launch your wsl instance.
$ API_KEY=123
$ echo $API_KEY
should print yourAPI_KEY
.
WSL Linux set env variable permanently from a bash terminal
- Launch your wsl instance.
$ sudo vim ~/.bashrc
- Enter your password.
- Press
i
to go into edit mode. Go to the end of the file using arrow key. - Add your variable as
API_KEY=123
at the end of the file. If your variable has spaces, use quotes. Example -API_KEY= 'My Key'
- Press
esc
key to get out of edit mode. - Enter
:wq
and press enter . This will save and close the file. $ source ~/.bashrc
will load your recent changes into your current shell.$ echo $API_KEY
should print yourAPI_KEY
.
This post was originally published at https://www.jskap.com/blog/set-environment-variables-windows-wsl-linux/
👋 Hi! I’m Kapil. I am always chatty about building things, sharing my learnings, freelancing. Come say hi to me at https://twitter.com/kapilgorve