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.

  1. Home
  2. Bash Dictionary
  3. export / env / printenv

export / env / printenv

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.

env_setup.sh
export APP_ENV=production
export DB_HOST=localhost
echo "APP_ENV: $APP_ENV"
echo "DB_HOST: $DB_HOST"
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.

export_later.sh
VERSION="1.2.3"
export VERSION
echo "VERSION: $VERSION"
bash export_later.sh
VERSION: 1.2.3

Check common environment variables.

show_env.sh
echo "User: $USER"
echo "Home: $HOME"
echo "Shell: $SHELL"
echo "Current dir: $PWD"
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.

path_add.sh
export PATH="$HOME/bin:$PATH"
echo "First entry in PATH: $(echo $PATH | cut -d: -f1)"
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

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 .