Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Use the Linux export Command

Set environment variables that child processes can use, and learn how to view, remove and keep them.

How-To Guides2 min read4 steps4 screenshots

Environment variables

An environment variable is a named value that programs can read, such as PATH or HOME. A normal shell variable exists only in the current shell. export marks a variable so that every program started from this shell can also see it.

Basic use

export variable_name=value

Example:

export MY_NAME="John"
Exporting a variable

Do not put spaces around =.

Read the variable

echo $MY_NAME
Reading the exported variable

Export a function (Bash)

function name() {
  echo "Hello, $MY_NAME!"
}
export -f name

View all exported variables

export
Listing the exported variables

You can also use export -p or printenv.

Remove a variable

unset MY_NAME
Removing the variable with unset

Common uses

  1. Add a folder to PATH, the list of folders searched for commands:
export PATH=$PATH:/path/to/bin
  1. Set language and locale:
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
  1. Give settings to scripts:
export DATABASE_HOST=localhost
export DATABASE_USER=myuser
  1. Set a shell alias. This is not an export, but it is often put in the same file:
alias ll='ls -la'

Make it permanent

An exported variable lasts only for the current session. To keep it, add the line to a startup file:

  • ~/.bashrc for your interactive shells.
  • ~/.profile for login shells.
  • /etc/environment for all users (plain NAME=value lines, no export).

Then reload the file:

source ~/.bashrc

Quick recap

  • export NAME=value shares a variable with child programs.
  • unset NAME removes it.
  • Add it to ~/.bashrc to keep it after logout.