How to Use the Export Command in Linux
Published on
•3 min read

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:
export [-f] [-n] [name[=value] …]Or to list exported variables:
export -pWhere:
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:
export -pExample 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:
export VARIABLE=VALUEVariable names are case-sensitive. By convention, environment variables should have UPPER CASE names.
Example:
export COUNT=2024Check its value:
echo $COUNT2024Variables 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:
COUNT=2024
export COUNTExporting Functions
Functions can be exported to child shells using the -f option. Example:
greet() {
echo “Hello World!”
}export -f greet-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:
greetHello 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:
export -n VARIABLETo remove a variable completely from both current and child processes:
unset VARIABLEMake 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:
export JAVA_HOME="/path/to/java/home"
export PATH=$PATH:$JAVA_HOME/binConclusion
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 -ffor functions. - Remove exports with
export -nor completely delete withunset. - Persistent exports require adding them to the shell configuration files.
Mastering export will make your shell scripts and session management much more powerful.


