export Command

The export command sets environment variables and makes them available to child processes. It converts shell variables into environment variables that can be inherited by subprocesses.

Syntax

export [OPTION] [NAME[=VALUE]]...

Description

The export command is a shell builtin that marks variables for automatic export to the environment of subsequently executed commands. It's essential for making variables available to child processes.

Key features:

  • Convert shell variables to environment variables
  • Make variables available to child processes
  • Display all exported variables
  • Set and export variables in one command
  • Essential for shell scripting and configuration

Common Options

Option Description
-p Display all exported variables in a format that can be reused
-n Remove the export property from variables
-f Export functions instead of variables

Examples

Export a new variable

export MY_VAR="Hello World"

Creates and exports a new environment variable

Export existing shell variable

MY_VAR="Hello World" export MY_VAR

Exports an existing shell variable to the environment

Display all exported variables

export

Shows all currently exported environment variables

Display in reusable format

export -p

Shows exported variables in a format that can be sourced

Export PATH variable

export PATH="/usr/local/bin:$PATH"

Adds a directory to the PATH environment variable

Export multiple variables

export VAR1="value1" VAR2="value2" VAR3="value3"

Exports multiple variables in one command

Remove export property

export -n MY_VAR

Removes the export property from a variable (keeps it as shell variable)

Export with command substitution

export CURRENT_DATE=$(date)

Exports a variable with the result of command substitution

Export for current session

export EDITOR=vim export BROWSER=firefox

Sets common environment variables for the current session

Export function

my_function() { echo "Hello from function"; } export -f my_function

Exports a shell function to child processes

Common Use Cases

Setting development environment

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk export MAVEN_HOME=/opt/maven export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

Common setup for Java development environment

Database configuration

export DB_HOST=localhost export DB_PORT=5432 export DB_NAME=myapp export DB_USER=appuser

Setting database connection parameters

Application configuration

export NODE_ENV=production export PORT=3000 export DEBUG=false

Common application environment variables

See also