🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

export Command

Beginner Shell Scripting man(1)

Set environment variables for child processes

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
export [NAME[=VALUE]]...

What Does export Do?

export marks a shell variable so it is passed to child processes as an environment variable. Without export, variables are local to the current shell and invisible to any commands or scripts you run.

export is essential for setting up environment variables that programs need to function — like PATH, HOME, and custom application settings. Variables set with export persist for the duration of the shell session.

For permanent environment variables, add export commands to shell initialization files like ~/.bashrc, ~/.bash_profile, or /etc/environment.

Options & Flags

OptionDescriptionExample
-n Un-export a variable (keep as local) export -n MY_VAR
-p List all exported variables export -p
-f Export a function export -f my_function
VAR=value Set and export in one step export PATH="$HOME/bin:$PATH"

Practical Examples

#1 Set environment variable

Sets and exports DB_HOST so child processes can read it.
$ export DB_HOST="localhost"

#2 Extend PATH

Adds a custom directory to the beginning of PATH.
$ export PATH="$HOME/.local/bin:$PATH"

#3 Set multiple variables

Exports multiple variables in one command.
$ export NODE_ENV="production" PORT=3000

#4 Check exported variables

Lists all exported variables matching DB_.
$ export -p | grep DB_
Output: declare -x DB_HOST="localhost"

#5 Un-export a variable

Removes export attribute — variable remains in shell but is not passed to children.
$ export -n SECRET_KEY

#6 Permanent export

Adds export to .bashrc so it persists across sessions.
$ echo 'export JAVA_HOME="/usr/lib/jvm/java-17"' >> ~/.bashrc

Tips & Best Practices

Difference: VAR=x vs export VAR=x: VAR=x sets a local shell variable. export VAR=x makes it available to child processes. Use export when programs need to read it.
Inline environment: Set a variable for a single command: DB_HOST=remote ./script.sh. This exports DB_HOST only for that command.
Not persistent by default: export only lasts for the current session. For persistence, add it to ~/.bashrc or ~/.profile.

Frequently Asked Questions

What does export do?
export makes a shell variable available to child processes (programs, scripts). Without export, variables are local to the current shell only.
How do I make an environment variable permanent?
Add the export command to ~/.bashrc (for interactive shells) or ~/.profile (for login shells). Then run source ~/.bashrc to apply immediately.
What is the difference between export and env?
export sets and marks variables for export. env shows current environment variables or runs a command with modified environment.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →