Defining a variable with or without export

Learn defining a variable with or without export with practical examples, diagrams, and best practices. Covers linux, bash, shell development techniques with visual explanations.

Understanding 'export' in Bash: Local vs. Environment Variables

Hero image for Defining a variable with or without export

Explore the crucial difference between defining a variable with and without export in Bash, and how it impacts variable scope in shell scripts and subshells.

In the world of Linux and Bash scripting, managing variables is a fundamental skill. A common point of confusion for beginners and even experienced users is the distinction between defining a variable directly (e.g., MY_VAR="value") and defining it with the export command (e.g., export MY_VAR="value"). This article will demystify the export keyword, explain its impact on variable scope, and provide practical examples to illustrate when and why you should use it.

The Basics: Local Variables

When you define a variable in Bash without using export, it becomes a local variable. This means its scope is limited to the current shell session or the current script where it was defined. Subshells or child processes spawned from this shell will not inherit these local variables. They exist only within the process that created them.

VAR_LOCAL="I am local"
echo "Parent shell: $VAR_LOCAL"

bash -c 'echo "Subshell: $VAR_LOCAL"'

Demonstrating a local variable's scope

As you'll observe from the output of the code above, the subshell does not have access to VAR_LOCAL. This behavior is by design and is often desirable for encapsulating variables within specific processes.

Extending Scope: Environment Variables with 'export'

The export command changes a local variable into an environment variable. Environment variables are special because they are automatically inherited by any child processes or subshells that are spawned from the current shell. This makes them accessible across different programs and scripts executed within that environment.

export VAR_EXPORTED="I am exported"
echo "Parent shell: $VAR_EXPORTED"

bash -c 'echo "Subshell: $VAR_EXPORTED"'

Demonstrating an exported variable's scope

In this case, the subshell will be able to access VAR_EXPORTED. This is crucial for tasks like setting PATH, LD_LIBRARY_PATH, or custom configuration variables that need to be available to programs launched from your shell.

flowchart TD
    A[Parent Shell] --> B{Define Variable}
    B -- "No export" --> C[Local Variable]
    C -- "Not inherited" --> D[Subshell]
    B -- "With export" --> E[Environment Variable]
    E -- "Inherited" --> D
    D[Subshell] --> F{Access Variable}
    C -- "Cannot access" --> F
    E -- "Can access" --> F

Variable scope with and without 'export'

When to Use Which

The choice between a local and an environment variable depends entirely on your needs:

  • Use local variables for temporary data, loop counters, or any variable whose scope should be confined to the current script or function. This helps prevent unintended side effects and keeps your environment clean.
  • Use environment variables (with export) when you need a variable to be accessible by child processes, other scripts, or programs launched from your current shell. Common examples include PATH, HOME, USER, and any custom configuration variables for applications.
# Define a local variable within a function
my_function() {
  local func_var="Inside function"
  echo "Function: $func_var"
}

my_function
echo "Outside function: $func_var" # This will be empty

# Export a variable for a specific command
export TEMP_DIR="/tmp/my_app" && my_app_script.sh

# Or set and export in one go
export MY_CONFIG="production"

Practical examples of local and exported variables