logo

How to set environment variable?

Posted on
Authors

Solutions

ActionmacOS/Linux shellPowershell
List all environment variablesenv or printenv or setdir env:
Reference a variable$name or ${name}$env:name
Print the value of the environment variableecho $nameecho $env:name
Set local environment variableCurrent process only: name=value or set name=value
Current process and its child processes: export name=value
$env:name = 'value'
Set global environment variablePlace an export command in your shell’s startup script of your home directory:
  • ~/.bashrc
  • ~/.bash_profile
  • ~/.zshrc
  • ~/.zsh_profile
  • ~/.profile
or /etc/profile for system-wide operations.
User Variable: [Environment]::SetEnvironmentVariable("name", "value" ,"User")
Machine Variable: [Environment]::SetEnvironmentVariable("name", "value" ,"Machine")
Unsetname= or unset nameUser Variable: [Environment]::SetEnvironmentVariable("name", $null ,"User")
Machine Variable: [Environment]::SetEnvironmentVariable("name", $null ,"Machine")

Where:

  • name: The name of the environment variable you want to set.
  • value: The value you want to assign to the new environment variable.

Linux/macOS

  • Environment variables in macOS/Unixes are case-sensitive.
  • Global environment variables (available to ALL processes) are named in uppercase, with words joined with underscore (_), e.g., JAVA_HOME.
  • Local variables (available to the current process only) are in lowercase.

Windows

  • Environment variables are NOT case-sensitive (because the legacy DOS is NOT case-sensitive).
  • Environment variables are typically named in uppercase, with words joined with underscore (_), e.g., JAVA_HOME.