Managing Environment Variables with export in Linux

Understanding the export Command

The export command in Linux marks Shell variables as environment variables, making them accessible to child processes spawned from the current Shell sestion. This capability is fundamental for configuring application behavior, managing system paths, and sharing configuration data across processes.

How export Works

When you define a variable in Bash without export, it remains a Shell local variable. Such variables exist only within the current Shell and are not inherited by subshells or external commands.

GREETING="Welcome"

Applying export transforms this into an environment variable:

export GREETING
export MESSAGE="Hello World"

The second form demonstrates the shorthand syntax—setting and exporting in a single command.

Practical Examples

Variable Inheritance Demonstration

# Define without exporting
APP_NAME="myapp"

# Local access works
echo $APP_NAME
# Output: myapp

# Subprocess cannot see it
bash -c 'echo $APP_NAME'
# Output: (empty)

# Export the variable
export APP_NAME

# Now child processes can access it
bash -c 'echo $APP_NAME'
# Output: myapp

Extending the PATH Variable

The PATH variable contains directories searched for executables. Adding custom locations requires exporting the modified value:

# Append a custom binary directory
export PATH=$PATH:/opt/custom/bin

# Verify the change
echo $PATH

Ephemeral Variables for Single Commands

Prefix a command with variable definitions to set temporary values that vanish after execution:

# Temporary environment for one command
export DEBUG_MODE=1 && ./my_program
# DEBUG_MODE exists only during this command

# After execution, the variable is gone
echo $DEBUG_MODE
# Output: (empty)

Administrative Operations

Viewing Exported Variables

Running export without arguments displays all currently exported variables:

export

This output shows each variable in the format declare -x NAME="value".

Removing Exported Variables

The unset command removes both Shell and environment variables:

export TEMP_VAR="data"
echo $TEMP_VAR
# Output: data

unset TEMP_VAR
echo $TEMP_VAR
# Output: (empty)

Common Use Cases

Configuring Language Runtimes

Software installations typically require environment configuration:

export JAVA_HOME=/usr/lib/jvm/java-17
export PATH=$PATH:$JAVA_HOME/bin
export LD_LIBRARY_PATH=$JAVA_HOME/lib:$LD_LIBRARY_PATH

Shell Script Initializasion

Scripts often need to communicate configuration to external programs:

#!/bin/bash

SERVICE_DIR="/opt/myservice"
export LOG_LEVEL="debug"
export CONFIG_PATH="$SERVICE_DIR/config/application.conf"

./start_service

Variable Display Formats

When using export -p, variables display with explicit declaration syntax:

export -p
# Output: declare -x HOME="/home/user"
# Output: declare -x SHELL="/bin/bash"

Making Persistent Changes

Exported variables exist only within the current Shell session. To preserve them across logins, add export commands to shell initialization files:

  • ~/.bashrc — loaded for interactive non-login shells
  • ~/.bash_profile or ~/.profile — loaded for login shells
echo 'export CUSTOM_VAR="persistent_value"' >> ~/.bashrc

Running source ~/.bashrc applies changes to the current session immediately.

Tags: Linux bash environment-variables Shell export

Posted on Thu, 10 Sep 2026 16:40:43 +0000 by will