export Command in Linux: Set Bash Environment Variables

By 

Updated on

5 min read

Developer using the export command in a Linux terminal beside the Tux mascot

You set a variable in your terminal, run a script that needs it, and the script sees nothing. By default, variables exist only in the shell where they are created. The export command turns a variable into an environment variable, meaning it is passed to child processes such as scripts, programs, and new shells started from your current terminal.

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 name-value pairs associated with a process. Child processes inherit a copy of their parent’s environment when they start.

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 locale settings.
  • SHELL - The path of the current user’s shell, such as bash or zsh.

Syntax Overview

The syntax of the export command is:

txt
export [-f] [-n] [name[=value] ...]

Or to list exported variables:

txt
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 will not be inherited by child processes).
  • -f - treats the specified name(s) as functions instead of variables.

To see why export matters, compare a regular shell variable with an exported one:

Terminal
NAME="linuxize"
bash -c 'echo "$NAME"'

export NAME="linuxize"
bash -c 'echo "$NAME"'

The first command does not pass NAME to the child shell. After export, the child shell can read it.

List All Exported Variables

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

Terminal
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:

Terminal
export VARIABLE=VALUE

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

Example:

Terminal
export COUNT=2024

Check its value:

Terminal
echo $COUNT
output
2024

Variables defined without export are local to the current shell and will not be inherited by child processes.

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

Terminal
COUNT=2024
export COUNT

Exporting Functions

Functions can be exported to child Bash shells using the -f option. If you are new to writing functions, our guide on Bash functions covers how to define them. Example:

Terminal
greet() {
  echo "Hello World!"
}

Export the function, then run it in a child Bash process:

Terminal
export -f greet
bash -c 'greet'
output
Hello World!

If you run bash -c 'greet' before exporting the function, the child shell reports that greet is not a command. The function exists only in the current shell until you export it.

Info
Without -f, export assumes you are exporting a variable, not a function.

Function export is a Bash feature and is not portable across all shells.

Remove Export Property

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

Terminal
export -n VARIABLE

To remove a variable from the current shell and prevent future child processes from inheriting it:

Terminal
unset VARIABLE

Processes that are already running keep the copy of the variable they inherited when they started.

Make Environment Variables Permanent

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

To load environment variables in future sessions, add the assignment to the file Bash reads for the type of shell you use:

  • ~/.bashrc - Interactive non-login Bash shells.
  • ~/.bash_profile - Login Bash shells.
  • /etc/profile - System-wide settings for login shells.
  • /etc/environment - System-wide session variables on systems that read this file. Use plain VARIABLE=value entries rather than shell commands.

If you are not sure whether to put the line in ~/.bashrc or ~/.bash_profile, our guide on .bashrc vs .bash_profile explains which file each shell reads.

For example, you can add these lines to the appropriate Bash startup file:

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

After editing a Bash startup file, use the source command to load that same file into your current shell. For example, after editing ~/.bashrc, run:

Terminal
source ~/.bashrc

If you edited ~/.bash_profile, source that file instead. Changes to /etc/environment normally take effect when you start a new login session. Other shells, including zsh, use their own startup files.

FAQ

What does the export command do in Linux?
export gives a shell variable the export attribute, so subsequently executed commands receive it in their environment. In Bash, export -f can also mark a function for inheritance by child Bash shells. Without export, the variable stays in the shell where it was defined.

Does the export command work in zsh?
The basic forms behave the same: export NAME=value and export -p work as they do in Bash. However, zsh does not support export -n, and export -f prints the function definition instead of exporting it. Exporting functions is a Bash-specific feature.

How do I make an exported variable permanent?
Add the assignment to the startup file your shell reads, such as ~/.bashrc for interactive non-login Bash shells. Reload that file with source or start a new shell. Variables exported only on the command line do not carry over to unrelated future sessions.

Conclusion

The export command marks Bash variables, and with -f functions, for inheritance by subsequently executed commands. Put variable assignments in the appropriate startup file when you need them in future sessions, and see our guide on setting and listing environment variables in Linux for a broader overview.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page