Bash Export Variable

Bash is one of the most commonly used shells in Unix-based operating systems. It provides a command-line interface that allows users to interact with the system and execute various commands. One of Bash's key features is the ability to export variables, making them available to child processes as environment variables.

In this article, we will explore the fundamentals of exporting variables in Bash, including syntax, usage patterns, and practical examples for system administration and scripting tasks.

What is a Bash Export Variable?

A Bash export variable is a variable that is made available to all child processes of the current shell. When a variable is exported, it becomes an environment variable, which means it can be accessed by any program or script that runs in the current shell environment.

Variables in Bash exist in two forms:

  • Shell variables Available only to the current shell

  • Environment variables Available to the shell and all its child processes

Basic Export Syntax

To export a variable in Bash, use the export command followed by the variable assignment:

export VARIABLE_NAME="value"

Single Variable Export

export MY_VARIABLE="Hello World"

Multiple Variables Export

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

Export Existing Variable

MY_VAR="test"
export MY_VAR

Viewing Exported Variables

Several commands can display environment variables:

# Display all environment variables
env

# Display all variables (shell + environment)
set

# Display only exported variables
export

# Search for specific variable
env | grep MY_VARIABLE

Practical Examples

Using Exported Variables in Scripts

#!/bin/bash

# Export variables for child processes
export DB_HOST="localhost"
export DB_PORT="5432"
export DEBUG_MODE="true"

# Script can access these variables
echo "Connecting to database at $DB_HOST:$DB_PORT"

# Child processes will inherit these variables
./another_script.sh
Connecting to database at localhost:5432

Command Substitution with Export

#!/bin/bash

# Export result of command execution
export CURRENT_DATE=$(date +%Y-%m-%d)
export SERVER_UPTIME=$(uptime -p)

echo "Today's date: $CURRENT_DATE"
echo "Server uptime: $SERVER_UPTIME"
Today's date: 2024-01-15
Server uptime: up 2 days, 5 hours, 23 minutes

Conditional Export

#!/bin/bash

# Export variable only if not already set
if [ -z "$PATH_BACKUP" ]; then
    export PATH_BACKUP="$PATH"
fi

# Export with default value
export LOG_LEVEL="${LOG_LEVEL:-INFO}"

Common Use Cases

Configuration Management

# Application configuration
export APP_ENV="production"
export API_KEY="your_secret_key"
export MAX_CONNECTIONS="100"

Path Management

# Add custom directories to PATH
export PATH="$HOME/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"

Profile Configuration

Add exports to ~/.bashrc or ~/.bash_profile for persistent variables:

# In ~/.bashrc
export EDITOR="vim"
export BROWSER="firefox"
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk"

Advanced Techniques

Temporary Environment for Commands

# Set variable for single command execution
MY_VAR="temporary" ./script.sh

# Multiple variables for single command
VAR1="value1" VAR2="value2" python script.py

Unsetting Variables

#!/bin/bash

export TEMP_VAR="temporary value"
echo "TEMP_VAR is set to: $TEMP_VAR"

# Remove the variable
unset TEMP_VAR
echo "TEMP_VAR after unset: $TEMP_VAR"
TEMP_VAR is set to: temporary value
TEMP_VAR after unset: 

Best Practices

Practice Example Reason
Use quotes for values export MSG="Hello, World!" Handles spaces and special characters
Use uppercase names export DATABASE_URL Convention for environment variables
Check if variable exists ${VAR:-default} Provides fallback values
Document critical exports # Database connection string Improves maintainability

Conclusion

Exporting variables in Bash is a fundamental technique for making data available across processes and scripts. Understanding how to properly export, view, and manage environment variables is essential for system administration, automation, and application deployment. The export command provides a simple yet powerful way to share configuration and data between shell sessions and their child processes.

Updated on: 2026-03-17T09:01:38+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements