export

Set a variable with export, read it with $NAME, list them all with env — and why programs only see exported ones.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • export APP_ENV=production
  • echo $APP_ENV
  • env | grep APP
  • printenv PATH
  • unset APP_ENV
  • export

How it works

An environment variable is a name with a value that programs can read: HOME, PATH, USER. export NAME=value creates one (no spaces around =).

Without export, NAME=value is only seen by your shell. With it, every script and program you start from here sees it too. It lasts until you close the terminal.

Syntax

export NAME=value  |  echo $NAME  |  unset NAME

Options

OptionWhat it does
NAME=valuecreate or change a variable (no spaces around =)
$NAMEthe value, wherever you write it
envlist every exported variable
unset NAMEremove it

Good to know

  • export NAME = value (with spaces) fails: the shell reads it as three separate words.
  • To keep a variable for every new terminal, put the export line in ~/.bashrc. Containers get theirs with docker run -e NAME=value.