export Command Cheat Sheet
The export command marks variables and functions to be passed to child processes. Without export, a variable defined in a shell is local to that shell and not visible to scripts or commands it runs.
Synopsis
export [-fn] [name[=value] ...]
export -p
Basic Usage
Export a Variable
Define a variable and export it in one step.
export MY_VAR="Hello World"
Export Existing Variable
Define first, export later.
MY_VAR="Hello World"
export MY_VAR
Viewing Exports
List All Exported Variables (-p)
Displays all variables currently exported to child processes.
export -p
Managing Exports
Un-export a Variable (-n)
Removes the "exported" property from a variable. The variable remains set in the current shell but will not be passed to child processes.
export -n MY_VAR
Export Functions (-f)
You can export Bash functions so they are available in subshells and scripts.
my_function() {
echo "I am available everywhere!"
}
export -f my_function
bash -c 'my_function'
Common Key Variables
| Variable | Description |
|---|---|
PATH |
Directories to search for commands |
HOME |
Current user's home directory |
USER |
Current username |
SHELL |
Path to current shell |
EDITOR |
Default text editor (e.g., vim, nano) |
LANG |
System locale and language |
PS1 |
Primary command prompt string |
Persistence
export only affects the current shell session. To make changes permanent:
For specific user:
Add to ~/.bashrc (interactive) or ~/.profile (login).
echo 'export PATH=$PATH:/opt/bin' >> ~/.bashrc
source ~/.bashrc
For all users:
Add to /etc/profile or a file in /etc/profile.d/.
Child Process Inheritance
Understanding scope is key.
1. Local Variable (Not Exported)
VAR="local"
bash -c 'echo $VAR'
# Output: (empty)
2. Exported Variable
export VAR="global"
bash -c 'echo $VAR'
# Output: global
3. Child cannot change Parent A child process receives a copy of the environment. Changes do not propagate back up.
export COUNTER=1
bash -c 'export COUNTER=5'
echo $COUNTER
# Output: 1
Examples
Setting PATH
Adding a directory to your command path is the most common use case.
export PATH=$PATH:/home/user/my_scripts
Setting Default Editor
export EDITOR=nano
Scripting with env
While export sets the variable for the current shell and all future children, env sets it for just one command.
# Permanent for session
export DEBUG=1
./script.sh
# Temporary for one command
env DEBUG=1 ./script.sh
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| >0 | Invalid option or invalid variable name |
Notes
exportis a shell builtin.- Variable names must start with a letter or underscore, and contain only letters, numbers, and underscores.
- Security: Be careful exporting sensitive data (like API keys) if you are running untrusted scripts, as they can read any exported variable.