Linux export Command with Examples

May 26, 2022


The export command is a built-in Bash shell command that exports environmental variables as child processes without affecting the existing environment variables. Local shell variables are known only to the shell that created them, and starting a new shell session means that previously created variables are unknown to it.

Use the export command to export the variables from a shell, making them global and available in each new shell session.

In this tutorial, you will learn to use the export command and see useful command examples.

How to use the Linux export command - see examples.


  • A system running Linux.
  • Access to a terminal (Ctrl + Alt + T).

Linux export Syntax

The syntax for the export command is:

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


export -p

Running the command without any options outputs all the exported variables and functions:

Linux export command output.

Exit Status

The command runs successfully unless you pass an invalid option or an invalid variable [name].

Linux export Options

The options allow users to remove, add, or see previously exported variables. The following table shows the available export options:

-fExports [name]s as functions.
-nAllows users to remove [name]s from the list of exported variables.
-pDisplays a list of all exported variables and functions in the current shell.

For example:

1. Using the -p option:

The -p option lists all variable names used in the current shell. Run:

export -p
Using the -p option in the export command.

The output lists all the variables used in the current shell session, and it is usually the same as running export without options.

2. Using the -f option:

The -f option exports the variable names as functions. To export a name as a function, create a function in the command line with a unique name. After exporting it, call the function using its name in the command line.

Follow the steps below to create and export a function:

1. Create and call the function by running:

function print_msg {
echo "Hello world"
Creating and calling a function in Linux.

2. Export the function using the -f option:

export -f print_msg

3. Start a new child shell session and call the function name:

Exporting a function in Linux using the export command.

The function works even after starting a child shell as it was previously exported.

3. Using the -n option:

The -n option removes the specified variables and functions from the list of exported variables. In the following example, we remove the EDITOR variable:

export -n EDITOR

Using the grep command to search for the EDITOR variable in the export output prints no results as the variable has been removed:

export | grep EDITOR
Removing an exported variable using the export command.

Linux export Command Examples

The following section showcases useful examples of using the export command.

Assign a Value Before Exporting

Assign a value to a variable before exporting it using the export command. For example:

x = 15

After setting the value, export the variable:

export x

Print the value of the variable using the echo command or printenv:

printenv x
Setting a variable value in Linux.

Export Multiple Variables

Export multiple variables by specifying them one after another. In the following example, we set three different environment variables and export them at the same time:

Exporting multiple variables in Linux.

Printing the variables outputs their value in the specified sequence.

Set a Default Value

Use the export command to assign the default value of an environment variable. For example, choose one of the 22 Best Linux text editors and change the default text editor to your preferred one.

Run the following command to set vim as the default text editor via the EDITOR variable:

export EDITOR= /usr/bin/vim

Check the value of the EDITOR variable by piping the export command output to grep and searching for the EDITOR string:

export | grep EDITOR
Setting the default text editor in Linux using the export command.

The output shows the value of the EDITOR variable, which points to the vim text editor path.

Change Command Line Appearance

Change and export the value of the primary prompt PS1 variable, which defines the Bash prompt's default structure, displaying it every time the user logs in using the terminal. The default values of PS1 are set in the /etc/bashrc file.

For example, change the prompt color by running:

export PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
Editing the PS1 variable value to change the command prompt's appearance.

The prompt color changes to green.

Export Name Value

Exporting a name value restricts it to the login shell level. Closing the command line or ending the shell session also removes the name value. Use the export command to export a name value. For example, set and export the JAVA_HOME environment variable using the following syntax:

export JAVA_HOME=/usr/share/java-x.x.x/

Replace the Java version number with the one installed on your system.

Note: Don't have Java on your system? See how to install Java on Ubuntu.

For example:

Setting the JAVA_HOME variable path using the export command.

Checking if the variable was set with echo outputs the JAVA_HOME path.

Another way to check is to grep the export command output:

export | grep JAVA_HOME
Using the grep command to search for a variable in export command output.

The output prints the variable contents, i.e., the JAVA_HOME environment variable path.


This tutorial showed how to use the Linux export command to export the value of environment variables and functions for use in shell sessions other than the current one. Next, read our tutorial on printing a variable in Bash with printf, or see other ways of exporting variables in Bash.

Was this article helpful?
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How To Run A Bash Script
December 9, 2021

Bash scripts store commands that often go together, such as updates and upgrades, to accomplish certain tasks automatically.
Read more
Linux set Command
November 23, 2021

On Unix-like operating systems, the set command functions within the Bourne shell (sh), C shell (csh), and Korn shell (ksh). In this tutorial, you will learn...
Read more
How to Comment in Bash
November 30, 2021

Comments are an essential part of programming. Learn how to use comments and the best practices to apply to your Bash script commenting techniques.
Read more
How To Check If File or Directory Exists in Bash
November 29, 2023

Searching for specific files or directories can be time-consuming. You can use a bash command or script to streamline the process. This...
Read more