Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An exported variable in Bash is a shell variable that has been marked for inclusion in the environment block of subsequently executed child processes. By default, standard shell variables are local to the current shell instance and are not inherited by external commands or independent scripts. Applying the export attribute to a variable promotes it to an environment variable, ensuring its identifier and value are passed to any process spawned from that shell via the execve system call.

Syntax

Bash provides multiple ways to apply the export attribute to a variable:

# 1. Declaration and assignment in a single statement
export VAR_NAME="value"


# 2. Applying the export attribute to an existing shell variable
VAR_NAME="value"
export VAR_NAME


# 3. Using the declare builtin with the export (-x) flag
declare -x VAR_NAME="value"

Process Mechanics and Scope

  • Unidirectional Inheritance: When a child process is created (typically via fork and exec), it receives a deep copy of the parent’s exported environment. Because it is a copy, any modifications made to the variable within the child process are strictly local to that child and do not propagate back to the parent shell.
  • Subshell Behavior: Subshells (created using ( ), command substitution $(), or background tasks &) inherit all variables from the parent shell’s memory space, regardless of the export attribute, because they are created via fork without exec. External commands and scripts executed as separate processes, however, strictly require the export attribute to access the variable.
  • Per-Command Exporting: Bash allows a variable to be exported temporarily to the environment of a single command without modifying the parent shell’s environment state.

# VAR_NAME is placed in the environment block exclusively for 'command'
VAR_NAME="value" command arg1 arg2

Attribute Management

The export attribute modifies the state of a variable, but it can be manipulated or removed independently of the variable’s value.

# Print all variables in the current shell that possess the export attribute
export -p

# Alternatively:
declare -x


# Remove the export attribute (demotes it back to a local shell variable)
export -n VAR_NAME


# Destroy the variable entirely (removes it from both the shell and the environment)
unset VAR_NAME

POSIX Compliance Note

While export VAR="value" is valid in Bash and POSIX-compliant shells, the declare -x syntax is a Bash-specific extension and will fail in strict POSIX environments (like sh or dash).
Master Bash with Deep Grasping Methodology!Learn More