All You Need to Know About Linux Export Command
The export
command in Linux is a built-in shell command used to set environment variables in the current shell session.
By marking variables or functions for subsequent export to the environment of child processes, the export
command ensures the availability of these variables to the child processes.
- 1 Syntax of the export command
- 2 Exporting a single environment variable
- 3 Exporting multiple environment variables
- 4 Exporting a function
- 5 Export command with no arguments
- 6 Remove variables from the environment
- 7 Display variables for usage as input
- 8 Verbose mode
- 9 Exporting variables safely
- 10 Use cases for export in real-world scenarios
- 11 Resources
Syntax of the export command
The basic syntax of the export
command is as follows:
export [options] [name[=value]]
Where name
is the name of the variable you want to set, value
is the value you want to assign to this variable, and options
are the flags or switches you can use with the command to modify its behavior.
Exporting a single environment variable
Let’s say you want to create an environment variable TEMP_DIR
and set its value to /tmp
. Here’s how you can do it:
export TEMP_DIR=/tmp
You can confirm that the variable is set correctly by echoing its value:
echo $TEMP_DIR
Output:
/tmp
In the above series of commands, you first use the export
command to create a new environment variable TEMP_DIR
and set its value to /tmp
.
The echo
command is then used to print the value of the TEMP_DIR
environment variable, and it correctly prints /tmp
confirming that the variable was set successfully.
Exporting multiple environment variables
You export more than one environment variable at a time by specifying each variable and its value in a single export command, separated by a space:
export VAR1="Variable 1" VAR2="Variable 2"
To check that the variables have been set correctly, use the echo command:
echo $VAR1 $VAR2
Output:
Variable 1 Variable 2
In the given command, we exported two environment variables, VAR1
and VAR2
, with values "Variable 1"
and "Variable 2"
respectively.
Exporting a function
You can use the export
command to export a function using -f
option. This is useful when you want to make a function available to subshells. Here’s how to do it:
First, define a function:
hello() { echo "Hello, world!" }
Then, export it:
export -f hello
You can check the exported function with declare -F
:
declare -F hello
Output:
hello
Initially, a function named hello
was defined, which when called, prints "Hello, world!"
.
The function was then exported using export -f hello
.
The -f
option is used to export functions. We used declare -F hello
to check if the function hello
was successfully exported.
The export -f
allows exporting multiple functions at once, rather than having to export each one individually.
function func1 { echo "This is function 1" } function func2 { echo "This is function 2" } function func3 { echo "This is function 3" } export -f func1 func2 func3 # Now functions can be called from subshells bash -c 'func1' # Outputs "This is function 1" bash -c 'func2' # Outputs "This is function 2" bash -c 'func3' # Outputs "This is function 3"
Export command with no arguments
By using the export
command without any arguments, you can display all the environment variables that have been set in your current shell session. Here’s an example:
export
Output:
declare -x HOME="/home/user" declare -x LANG="en_US.UTF-8" declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ...
In this instance, the export
command is executed without any arguments, resulting in the shell printing all the environment variables set in the current session.
Variables like HOME
, LANG
, and PATH
, among others, are typically predefined by the system.
Remove variables from the environment
The -n
option is used with the export
command to remove a variable from the environment.
Let’s say you want to remove the variable VAR1
that was previously defined. Here’s how you can do it:
export -n VAR1
To verify that the variable has been removed, try to echo its value:
echo $VAR1
Output:
# No output
We used the -n
option with the export
command to remove the VAR1
variable. When we try to display its value using the echo $VAR1
command, there is no output.
Display variables for usage as input
The -p
option with the export
command prints the list of exported variables and functions in a format that can be reused as input. Here’s how you can use it:
export -p
Output:
declare -x HOME="/home/user" declare -x LANG="en_US.UTF-8" declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ... declare -f hello
Here, export -p
is used to list the exported variables and functions.
The output format is such that it can be readily used as input for other shell scripts.
Variables are preceded by declare -x
and functions by declare -f
, helping us differentiate between them.
Verbose mode
The -v
option in conjunction with the export
command gives a verbose output when exporting variables.
This option is helpful for debugging as it prints a message for each exported variable.
Let’s try exporting a variable VAR3
in verbose mode:
export -v VAR3="Verbose Variable"
Output:
export VAR3='Verbose Variable'
In this command, VAR3
is exported with the value "Verbose Variable"
.
The -v
option triggers a verbose output that shows the variable being exported along with its value.
Exporting variables safely
When exporting variables, especially those containing special characters or white spaces, it’s crucial to handle them safely.
To safely export a variable, you should enclose the value in quotes. This ensures that the entire string is treated as a single value, even if it contains spaces or special characters:
export SAFE_VAR="This is a 'safe' variable"
To check the value of the variable:
echo $SAFE_VAR
Output:
This is a 'safe' variable
In this case, the variable SAFE_VAR
is exported with a value that includes white spaces and a special character (single quotes).
By enclosing the value within double quotes, we make sure it’s interpreted as one single string.
Use cases for export in real-world scenarios
In real-world applications, the export
command is commonly used to set environment variables that affect system behavior or software configuration.
A common use case is modifying the PATH
environment variable to include directories containing executable files. For example:
export PATH=$PATH:/opt/myapp/bin
In this scenario, /opt/myapp/bin
is appended to the existing PATH
. Now, any executable in /opt/myapp/bin
can be run from any location without specifying the full path.
Resources
https://www.man7.org/linux/man-pages/man1/export.1p.html
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.