The export
command in Linux marks environment variables for export to child processes. When you set a variable in a shell session without exporting it, that variable remains local to the current shell and won't be inherited by any commands or scripts you launch.
# Local variable (not available to child processes)
MY_VAR="hello"
# Exported variable (available to child processes)
export MY_VAR="world"
Here are three practical scenarios where you'd need export:
1. Setting PATH modifications:
export PATH=$PATH:/usr/local/custom/bin
2. Configuring application environments:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export ANDROID_HOME=$HOME/Android/Sdk
3. Temporary debugging flags:
export DEBUG=true
export LOG_LEVEL=verbose
Exported variables have specific inheritance rules:
- Available to all child processes of the current shell
- Not available to parent processes
- Not persistent across system reboots (unless added to shell config files)
To persist exported variables between sessions:
For bash users:
echo 'export MY_VAR="value"' >> ~/.bashrc
source ~/.bashrc
For zsh users:
echo 'export MY_VAR="value"' >> ~/.zshrc
source ~/.zshrc
Exporting functions:
myfunc() { echo "This works"; }
export -f myfunc
Listing all exported variables:
export -p
Removing an exported variable:
unset MY_VAR
- Spaces around equals sign when setting variables (wrong:
export VAR = value
) - Forgetting to export before running dependent commands
- Assuming exported variables persist after closing the terminal
In Linux systems, the export
command plays a crucial role in shell scripting by making variables available to child processes. When you declare a variable without exporting it, that variable remains local to the current shell session. However, exported variables become part of the environment and are inherited by any child processes.
# Local variable (not exported)
MY_VAR="local_value"
# Exported variable
export GLOBAL_VAR="available_to_children"
The most common uses of export
include:
# Setting PATH for child processes
export PATH=$PATH:/usr/local/custom_bin
# Configuring application-specific variables
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
# Passing variables to scripts
export CONFIG_FILE=/etc/myapp/settings.conf
./launch_script.sh
Exported variables set directly in the terminal are temporary and only last for the current session. For permanent environment variables, you need to add them to configuration files:
# For user-specific permanent variables
echo 'export EDITOR=vim' >> ~/.bashrc
# For system-wide variables (requires root)
echo 'export HTTP_PROXY=http://proxy.example.com:8080' >> /etc/profile
The export
command supports several useful options:
# Export a function
myfunc() { echo "Hello $1"; }
export -f myfunc
# Remove a variable from environment
export -n OLD_VAR
# List all exported variables
export -p
When working with export
, watch out for:
- Variable name collisions (use unique, descriptive names)
- Security risks when exporting sensitive data
- Proper quoting of values containing spaces
# Bad practice (spaces in value without quotes)
export BAD_VAR=value with spaces
# Good practice
export GOOD_VAR="value with spaces"