Set environment variables

Posted: | Tags: til

I usually forget the syntax of defining enviornment variables on different platforms, so here’s a note for future me to look up.1

Bash/Zsh

export VARIABLE_NAME=ABC123

Using export will set the environment variable within the current session, you can override the value by using export again on the same variable name. To apply this environment variable to all sessions set the variable within the shell’s startup script such as .bashrc or /etc/envrionment to be available by all users.

PowerShell

$Env:VARIABLE_NAME="ABC123"

Using $Env will set the environment variable within the current session, you can override the value by using $Env again on the same variable name. To apply this environment variable to all sessions set the variable in the “Environment Variables” field in the System Control Panel. If you’re using PowerShell on a Linux machine set the environment variable in /etc/environment or on macOS set the variable in the /etc/profile file. More details can be found in the PowerShell documentation.

Windows Command Prompt

set VARIABLE_NAME=ABC123

Using set will set the environment variable within the current session, you can ovveride the value by using set again on the same variable name. You can apply this environment variable to all sessions by using setx.

setx VARIABLE_NAME ABC123

  1. Apparently I’m not alone, this toot from Julia Evans shows there are others like me that forget the syntax for environment variable. ↩︎


Related ramblings