export Command Examples

Environment Export - Share Variables with Child Processes

About export

The export command is a shell builtin that marks shell variables for export to child processes. When you export a variable, it becomes part of the environment that's inherited by any programs or scripts you run from that shell. This is fundamental to configuring how applications behave in Unix/Linux systems. Understanding export is essential for shell scripting, system configuration, and application development.

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.

Use Case: Setting configuration variables that applications will read, creating custom environment settings, or passing data to scripts and programs.
Variable names are case-sensitive. Convention is to use UPPERCASE for environment variables.

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.

Use Case: Testing variable values in your current shell before making them available to other programs, or following shell script patterns where variables are set and exported separately.

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"

Use Case: Documenting your environment setup, debugging environment issues, or saving environment state for later restoration.
To save your environment: 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.

Use Case: Adding custom script directories, installing software in non-standard locations, or making compiled programs accessible from anywhere.
To prepend instead (higher priority): 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"

Use Case: Creating timestamped directories, generating dynamic configuration values, or capturing system state for scripts and applications.
The value is set once when you run the export. It won't automatically update. To get current date later, run the command again.

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.

Use Case: Setting up database connection parameters, configuring application settings, or initializing related environment variables as a group.

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.

Use Case: Creating utility functions in your .bashrc that are available in scripts, sharing complex shell logic across script invocations, or building reusable shell function libraries.
This is Bash-specific and won't work in sh, zsh (unless in bash compatibility mode), or other 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.

Use Case: Keeping a variable in your shell for local use while preventing it from being inherited by child processes, managing variable scope in complex scripts, or temporarily hiding variables.
To completely remove a variable: use 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}

Use Case: Providing fallback values in scripts, making configuration optional with sensible defaults, or preventing errors when variables might not be set.
Related: ${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.

Use Case: Testing scripts with different configurations, running a single command with debug mode, or experimenting with environment settings without polluting your shell.
For a single command, you can also use: 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
Common Patterns in .bashrc or .bash_profile:
# Add custom bin directory
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\$ "
Pro Scripting Pattern: Check if variable is set before exporting:
if [ -z "$MY_VAR" ]; then
  export MY_VAR="default_value"
fi
Security Warning: Never export sensitive data (passwords, API keys) in plain text in shell startup files. Use secure methods like:
  • Environment variable managers (direnv, dotenv)
  • Secret management tools (Vault, AWS Secrets Manager)
  • Encrypted configuration files
  • Read from secure files with restricted permissions
Debugging Exports: Check if a variable is exported:
$ declare -p MY_VAR
If output shows "declare -x", it's exported. If just "declare", it's not exported.
Shell Startup Files Priority (Bash):
  • 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
Common Mistake: Forgetting to re-source after editing startup files:
After editing ~/.bashrc or ~/.bash_profile, run:
source ~/.bashrc or . ~/.bashrc
Otherwise changes won't take effect until you start a new shell.
Variable Naming Best Practices:
  • 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