How to set environment variable?

Wed, 25 Jan 2023 08:50:51 GMT

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:

~/.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")

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.

Reference links

Environment Variables in Windows/macOS/Linux