How to set environment variable?
- Posted on
- Authors
- Name
- ansidev
- @ansidev
Solutions
Action | macOS/Linux shell | Powershell |
---|---|---|
List all environment variables | env or printenv or set | dir env: |
Reference a variable | $name or ${name} | $env:name |
Print the value of the environment variable | echo $name | echo $env:name |
Set local environment variable | Current process only: name=value or set name=value Current process and its child processes: export name=value | $env:name = 'value' |
Set global environment variable | Place an export command in your shell’s startup script of your home directory:
/etc/profile for system-wide operations. | User Variable: [Environment]::SetEnvironmentVariable("name", "value" ,"User") Machine Variable: [Environment]::SetEnvironmentVariable("name", "value" ,"Machine") |
Unset | name= or unset name | User 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.