Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active February 7, 2018 15:26
Show Gist options
  • Save CMCDragonkai/0b4b137d1b0bd03e1323 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/0b4b137d1b0bd03e1323 to your computer and use it in GitHub Desktop.
CLI: Setting & Unsetting Environment Variables on Linux & Windows

Windows

Show environment variable:

CMD

echo %ENVIRONMENT_VARIABLE%

Powershell

Get-ChildItem Env:ENVIRONMENT_VARIABLE

Show all environment variables:

CMD

SET

Powershell

Get-ChildItem Env:

Set the environment variable:

CMD

SET PYTHONHOME=C:\python33

PowerShell

$env:TestVariable = "This is a test environment variable."
[Environment]::SetEnvironmentVariable("TESTVARIABLE", "VALUE")

Permanent Setting & Unsetting:

Use setx http://technet.microsoft.com/en-us/library/cc755104.aspx

Create

setx FOOBAR 1

Clear

setx FOOBAR ""

Delete from User

REG delete HKCU\Environment /F /V FOOBAR

Delete from System

REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR

If you're running these commands from Cygwin, make sure to escape any backslashes.

This requires a restart on the terminal, even if you're in Cygwin.

Linux

Show environment variable:

echo $ENVIRONMENT_VARIABLE

Show all environment variables:

env

Set environment variable:

export ENVIRONMENT_VARIABLE="value"

Unset environment variable:

unset ENVIRONMENT_VARIABLE

Permanent Setting & Unsetting:

Use your initial script like .bashrc or .zshrc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment