Introduction
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.
Prerequisites
- 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] ...]
or
export -p
Running the command without any options outputs all the exported variables and functions:
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:
Option | Description |
---|---|
-f | Exports [name] s as functions. |
-n | Allows users to remove [name] s from the list of exported variables. |
-p | Displays 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
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"
}
print_msg
2. Export the function using the -f
option:
export -f print_msg
3. Start a new child shell session and call the function name:
bash
print_msg
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
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
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:
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
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\] '
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:
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
The output prints the variable contents, i.e., the JAVA_HOME
environment variable path.
Conclusion
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.