DEV Community

Kapil Gorve
Kapil Gorve

Posted on β€’ Originally published at jskap.com on

10 1

Set environment variable in Windows and WSL Linux in terminal

'cover'

Windows set env variable from the command line

  • Open command line. set API_KEY=123
  • echo %API_KEY% should print your API_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 your API_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 your API_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 your API_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

Top comments (0)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay