Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. export / env / printenv

export / env / printenv

Since: export All Linux
macOS(2001 Cheetah)
Bash 1.0(1989)
env / printenv All Linux
macOS(2001 Cheetah)
Bash 1.0(1989)

export passes shell variables to child processes as environment variables. Use env or printenv to view the current list of environment variables. Variables that control program behavior — such as $PATH, $HOME, and $USER — are set and inspected here.

Syntax

Export a variable as an environment variable.

export VARNAME=value
export VARNAME   # Export an already-defined variable

List all environment variables.

env
printenv

Display a specific environment variable.

printenv VARNAME
echo $VARNAME

Set an environment variable only for a single command.

VARNAME=value command

Commands and Variables

Command / VariableDescription
export VAR=valueDefines a variable and sets it as an environment variable.
export VARExports an existing shell variable so child processes can access it.
envLists all current environment variables.
printenvLists environment variables (similar to env).
printenv VARDisplays the value of the specified environment variable only.
unset VARRemoves an environment variable.
$PATHA colon-separated list of directories searched for commands.
$HOMEThe path to the current user's home directory.
$USERThe name of the currently logged-in user.
$SHELLThe path to the current shell.
$PWDThe path to the current working directory.
$LANGThe current locale setting.

Sample Code

Set environment variables with export and confirm their values.

sample_env_setup.sh
export APP_ENV=production
export DB_HOST=localhost
echo "APP_ENV: $APP_ENV"
echo "DB_HOST: $DB_HOST"

Run the following command:

bash env_setup.sh
APP_ENV: production
DB_HOST: localhost

You can also export an existing shell variable as an environment variable after the fact.

sample_export_later.sh
VERSION="1.2.3"
export VERSION
echo "VERSION: $VERSION"

The following example demonstrates this:

bash export_later.sh
VERSION: 1.2.3

Check common environment variables.

sample_show_env.sh
echo "User: $USER"
echo "Home: $HOME"
echo "Shell: $SHELL"
echo "Current dir: $PWD"

Run the following command:

bash show_env.sh
User: alice
Home: /home/alice
Shell: /bin/bash
Current dir: /home/alice/project

Add a new directory to $PATH. The convention is to prepend it while keeping the existing value intact.

sample_path_add.sh
export PATH="$HOME/bin:$PATH"
echo "First entry in PATH: $(echo $PATH | cut -d: -f1)"

Run the following command:

bash path_add.sh
First entry in PATH: /home/alice/bin

Set an environment variable only for a single command. It does not affect the rest of the script.

LANG=C ls --help 2>&1 | head -1
Usage: ls [OPTION]... [FILE]...

Use printenv to display the value of a specific environment variable.

printenv HOME
/home/alice
printenv SHELL
/bin/bash

Common Mistakes

Common Mistake 1: export in a child shell does not propagate to the parent

Variables exported in a child process (subshell or script) are not visible in the parent shell that called it.

bash set_var.sh
echo $MY_VAR
(empty — the variable was set only inside the script)

Use source (or .) to run the script in the current shell so the variable is available after.

source set_var.sh
echo $MY_VAR
hello

Common Mistake 2: Overwriting PATH entirely makes standard commands unavailable

Setting PATH without preserving its existing value removes all default command directories.

export PATH="/home/okabe/bin"
ls
bash: ls: command not found

Prepend the new directory while keeping the existing value.

export PATH="/home/okabe/bin:$PATH"

Notes

Shell variables are only available within the current shell, but once you export them, they are passed to child processes (subshells and external commands). By writing export statements in a configuration file (such as ~/.bashrc or ~/.bash_profile), environment variables are set automatically every time a new shell starts.

When adding to '$PATH', the convention is '$ export PATH="$HOME/bin:$PATH"' — prepend the new directory while preserving the existing value. Overwriting $PATH entirely will make standard commands unavailable.

For the basics of defining and referencing variables, see Defining and Referencing Variables.

If you find any errors or copyright issues, please .