export Command in Linux: Set Bash Environment Variables

By 

•

Updated on

•

7 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 to your login shell, such as /bin/bash or /usr/bin/zsh.

Syntax Overview

The syntax of the export command is:

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

Or to list exported variables and functions:

txt
export -p [-f]

Where:

  • name - The shell variable or function to export.
  • -p - Lists all exported variables in the current shell. With -f, it lists exported functions instead.
  • -n - Removes the export property from the specified variables. They remain defined in the current shell but are not inherited by child processes.
  • -f - Treats the specified names as functions instead of variables.

To see why export matters, compare a regular shell variable with an exported one. First, set NAME without export and read it from a child shell:

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

The command prints an empty line. A plain assignment does not pass NAME to the child shell.

To pass the variable to the child shell, export it and run the same command again:

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

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 HOME="/home/linuxize"
declare -x LANG="en_US.UTF-8"
declare -x OLDPWD
declare -x PATH="/usr/local/bin:/usr/bin:/bin"
declare -x PWD="/home/linuxize"
declare -x SHLVL="1"

These are variables that any child process will inherit. Bash sorts the list by name and prints each entry as a declare -x command. The -x option is the export attribute. OLDPWD has no value. It is marked for export, but child processes do not receive it until it is set.

You can also use printenv or the env command 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

A variable that is already exported keeps the export attribute when you change its value. PATH comes from the environment, so a plain assignment still reaches child processes:

Terminal
PATH="$PATH:/opt/app/bin"
bash -c 'echo "$PATH"'
output
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/app/bin

The child shell sees /opt/app/bin at the end of PATH, even though the assignment did not use export.

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 prints bash: line 1: greet: command not found. The function exists only in the current shell until you export it.

To list the functions exported from the current shell, run:

Terminal
export -pf
output
greet ()
{
    echo "Hello World!"
}
declare -fx greet

Bash prints the function body, followed by a declare -fx line. The f marks greet as a function, and the x marks it for export.

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.
  • ~/.profile - Login Bash shells when neither ~/.bash_profile nor ~/.bash_login is readable. Ubuntu and Debian use this file by default.
  • /etc/profile - System-wide settings for login shells.
  • /etc/profile.d/*.sh - System-wide scripts that /etc/profile runs at login. Add your own file here instead of editing /etc/profile.
  • /etc/environment - System-wide session variables on systems that read this file. Use plain VARIABLE=value entries rather than shell commands.

On Ubuntu and Debian, add your exports to ~/.profile and do not create a new ~/.bash_profile. When ~/.bash_profile exists, Bash stops reading ~/.profile for login shells.

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"

The second line adds the Java bin directory to the end of PATH. Our guide on adding a directory to PATH covers this pattern in more detail.

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. The export -p output also looks different, with export NAME=value lines instead of declare -x lines.

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.

How do I export a variable from a script to my current shell?
Run the script with source script.sh instead of bash script.sh. The bash form starts a child shell, and the exported variables disappear when that shell exits. A child process cannot change the environment of its parent. With source, the commands run in your current shell and the variables stay set after the script finishes.

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. The export cheatsheet keeps the common forms on one printable page.

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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page