1. Export a New Variable
$ export MY_VAR="Hello World"
Creates and exports a new environment variable in one step. The variable is now available to all child processes launched from this shell.
Effect: Any program or script you run will see MY_VAR in its environment.
2. Export Existing Variable
$ EDITOR=vim
$ export EDITOR
First creates a shell variable, then exports it. This two-step process is useful when you want to set a variable's value and then later decide to export it.
Before export: Variable only exists in current shell.
After export: Variable is inherited by child processes.
3. List All Exported Variables
$ export -p
Displays all currently exported variables in a format that can be re-executed as shell commands. Each line starts with "declare -x" or "export" depending on your shell.
Output format: declare -x PATH="/usr/bin:/bin"
export -p > my-env.txt and restore with source my-env.txt
4. Append to PATH
$ export PATH="$PATH:/new/directory"
Adds a new directory to the end of PATH without removing existing directories. The $PATH expands to the current value, then :/new/directory is appended.
Search order: Shell searches existing PATH directories first, then /new/directory.
export PATH="/new/directory:$PATH"
5. Export with Command Substitution
$ export CURRENT_DATE=$(date +%Y-%m-%d)
Uses command substitution to set a variable's value from command output. The $(date +%Y-%m-%d) is executed first, then its output becomes the variable's value.
Result: CURRENT_DATE will contain something like "2025-11-03"
6. Export Multiple Variables
$ export DB_HOST=localhost DB_PORT=5432 DB_NAME=mydb
Exports multiple variables in a single command by separating them with spaces. This is more concise than multiple export commands.
Equivalent to: Three separate export commands, one for each variable.
7. Export Function (Bash-specific)
$ my_function() { echo "Hello from function"; }
$ export -f my_function
The -f flag exports a shell function, making it available to child bash shells. This is a Bash-specific feature not available in all shells.
Limitation: Only works with Bash child processes, not with other programs or shells.
8. Unexport Variable (Remove from Environment)
$ export -n MY_VAR
The -n flag removes the export attribute from a variable. The variable still exists in your shell, but it's no longer exported to child processes.
Important distinction: This is different from unset which deletes the variable entirely.
unset MY_VAR instead
9. Export with Default Value
$ export PORT=${PORT:-8080}
Uses parameter expansion to set a default value. If PORT is already set, it keeps its current value. If PORT is unset or empty, it gets the value 8080.
Syntax: ${VARIABLE:-default_value}
${VAR:=default} also assigns the default to VAR if unset. ${VAR:-default} only uses default without assignment.
10. Temporary Export for Script
$ (export DEBUG=1; ./myscript.sh)
Uses a subshell (parentheses) to export a variable only for the duration of the command. The export affects only the subshell and its children, not your main shell.
After the command completes: Your shell's environment is unchanged. DEBUG is not set in your main shell.
DEBUG=1 ./myscript.sh without parentheses or export
export vs env vs set: Understanding the Differences
| Command | Scope | Duration | Best For |
|---|---|---|---|
| export VAR=val | Current shell + all children | Until shell exits | Persistent session config |
| VAR=val cmd | Only the command | Just that command | One-time override |
| env VAR=val cmd | Only the command | Just that command | Complex env modifications |
| VAR=val (no export) | Current shell only | Until unset or exit | Shell-internal variables |
Quick Reference: export Options
| Option/Syntax | Description |
|---|---|
export VAR=value |
Set and export variable in one step |
export VAR |
Export existing variable |
export -p |
List all exported variables |
export -n VAR |
Remove export attribute (keep variable) |
export -f func |
Export function (Bash only) |
export VAR1=val1 VAR2=val2 |
Export multiple variables at once |
export PATH="$HOME/bin:$PATH"
# Set default editor
export EDITOR=vim
export VISUAL=vim
# Development environment
export NODE_ENV=development
export PYTHONPATH="$HOME/python_modules"
# Database configuration
export DATABASE_URL="postgresql://localhost/mydb"
# Custom prompt
export PS1="\u@\h:\w\$ "
if [ -z "$MY_VAR" ]; then
export MY_VAR="default_value"
fi
- Environment variable managers (direnv, dotenv)
- Secret management tools (Vault, AWS Secrets Manager)
- Encrypted configuration files
- Read from secure files with restricted permissions
$ declare -p MY_VARIf output shows "declare -x", it's exported. If just "declare", it's not exported.
- Login shells: /etc/profile → ~/.bash_profile → ~/.bash_login → ~/.profile
- Interactive non-login: /etc/bash.bashrc → ~/.bashrc
- Common pattern: Put exports in ~/.bashrc, source it from ~/.bash_profile
After editing ~/.bashrc or ~/.bash_profile, run:
source ~/.bashrc or . ~/.bashrcOtherwise changes won't take effect until you start a new shell.
- Use UPPERCASE for exported environment variables
- Use lowercase for shell-local variables
- Use underscores for word separation: MY_CUSTOM_VAR
- Avoid special characters except underscore
- Start with letter or underscore, not numbers