Skip to main content

List, Get, Set, and Unset Mac and Linux Environment Variables in Terminal

Published: 2021-04-11 • Last updated: 2021-06-07

Note: This doc assumes you will use the Bash (bash) shell. You can check which shell you’re using by running:

/bin/ps -p ${$} -o comm=

List All Environment Variables

env

Get an Environment Variable

Syntax:

echo ${<VAR_NAME>}

Usage:

echo ${MY_NAME}

Set an Environment Variable

Syntax:

export <VAR_NAME>="<VAR_VALUE>"

Usage:

export MY_NAME="Jane Doe"

Persist on Mac:

Add export MY_NAME="Jane Doe" to your ~/.bash_profile file.

Then source it by running source ~/.bash_profile. Or, restart your Terminal.

Persist on Linux:

Add export MY_NAME="Jane Doe" to your ~/.bashrc file.

Then source it by running source ~/.bashrc. Or, restart your Terminal.

Unset an Environment Variable

Syntax:

unset <VAR_NAME>

Usage:

unset MY_NAME

Persist on Mac:

Add unset MY_NAME to your ~/.bash_profile file.

Then source it by running source ~/.bash_profile. Or, restart your Terminal.

Persist on Linux:

Add unset MY_NAME to your ~/.bashrc file.

Then source it by running source ~/.bashrc. Or, restart your Terminal.