How to Use the Export Command in Linux

Published on

3 min read

Bash Export

The export command is an essential part of the Bash shell in Linux. It allows you to share variables or functions you create so that they can be used by other programs or new shells started from your current terminal.

By default, variables exist only in the shell where they are created. When you use export, the variable becomes an environment variable, meaning it is passed to child processes.

In this article, we will show you how to use the export command to create, list, and manage environment variables and functions.

What Is an Environment Variable?

Environment variables are system-wide variables that are inherited by all spawned child processes and shells.

Environment variables allow you to customize how the system works and the behavior of the applications on the system. For example, environment variables can store information about the default text editor or browser, the paths to executable files, or the system locale and keyboard layout settings.

Common examples include:

  • PATH – Defines where the shell looks for executable files.
  • HOME – Your home directory.
  • USER – Your username.
  • LANG - The current locales settings.
  • SHELL - The path of the current user’s shell, such as bash or zsh.

Syntax Overview

The syntax of the export command is:

sh
export [-f] [-n] [name[=value]]

Or to list exported variables:

sh
export -p

Where:

  • name - the shell variable or function to export.
  • -p - lists all exported variables in the current shell.
  • -n - removes the export property from the specified variable(s) (they remain defined in the current shell but won’t be inherited by child processes).
  • -f - treats the specified name(s) as functions instead of variables.

List All Exported Variables

To see all exported variables in your current shell, type:

sh
export -p

Example output:

output
declare -x PATH=“/usr/local/bin:/usr/bin:/bin”
declare -x HOME=“/home/user”
declare -x LANG=“en_US.UTF-8”

These are variables that any child process will inherit.

You can also use printenv or env to see all currently active environment variables.

Creating and Exporting Environment Variables

To create a new environment variable , use the following form:

sh
export VARIABLE=VALUE

Variable names are case-sensitive. By convention, environment variables should have UPPER CASE names.

Example:

sh
export COUNT=2024

Check its value:

sh
echo $COUNT
output
2024

Variables defined without export are local to the current shell and won’t be inherited by child processes.

You can also first define a variable and export it separately:

sh
COUNT=2024
export COUNT

Exporting Functions

Functions can be exported to child shells using the -f option. Example:

sh
greet() {
echo “Hello World!”
}
Terminal
export -f greet
Info
Without -f, export assumes you’re exporting a variable, not a function.

Then, start a new child shell by typing bash, and enter the function name:

Terminal
greet
output:
Hello World!

If you haven’t exported the function, it will not be available in the child shell, and there will be no output.

Remove Export Property

To stop exporting a variable while retaining it in the current shell:

Terminal
export -n VARIABLE

To remove a variable completely from both current and child processes:

Terminal
unset VARIABLE

Make Environment Variables Permanent

Exported variables are not permanent and last only for the current session.

To make environment variables persistent across all sessions, add export VARIABLE=value to the shell configuration file. Environment variables are read from the following files:

  • /etc/environment
  • /etc/profile
  • ~/.bashrc
  • ~/.bash_profile

Example:

sh
export JAVA_HOME="/path/to/java/home"
export PATH=$PATH:$JAVA_HOME/bin

Conclusion

The export command is important for managing environment variables and functions in Linux.

Important aspects to remember:

  • Child processes inherit exported variables/functions.
  • Use export -f for functions.
  • Remove exports with export -n or completely delete with unset.
  • Persistent exports require adding them to the shell configuration files.

Mastering export will make your shell scripts and session management much more powerful.