export
Set environment variables for child processes
TLDR
Set an environment variable
Append a pathname to the environment variable PATH
SYNOPSIS
export [-fn] [name[=value] ...] or export -p
PARAMETERS
name[=value]
The name of the variable to export, optionally setting its value. If value is provided, the variable's value is set before marking it for export. If no name is given, all currently exported variables are displayed.
-f
Export the specified names as shell functions rather than variables. This makes the function definitions available to child shell processes.
-n
Remove the export property from the specified names. This makes the variables local to the current shell, preventing them from being passed to child processes.
-p
Display a list of all currently exported variables and functions in the current shell environment. This option should be used without any name or value arguments.
DESCRIPTION
The export command is a shell builtin that marks variables and functions to be passed into the environment of child processes. When a variable is exported, any new processes launched from the current shell will inherit a copy of that variable and its value. This is crucial for configuring programs, specifying paths, or passing data between different parts of a system.
Without export, a variable set in the current shell remains local to that shell instance and is not visible to commands or scripts executed from it. For instance, the PATH variable, which lists directories where the shell looks for executables, must be exported to ensure all commands can be found by subsequent processes. It is a fundamental command for managing the shell's environment and ensuring proper program execution.
CAVEATS
- Variables exported by export are inherited by child processes, but they do not affect the parent shell or sibling processes. Each shell runs in its own distinct environment.
- Values of exported variables are always treated as strings. If you assign a complex type (like an array in Bash), only the first element or a string representation might be exported, depending on the shell's behavior.
- Changes to an exported variable's value in a child process do not propagate back to the parent shell.
- The export command itself does not set a variable's value; it only marks an existing variable (or one being set simultaneously) for export.
VIEWING EXPORTED VARIABLES
To see all currently exported variables and their values, simply run export -p. Alternatively, env displays the environment variables that would be passed to a new process, and printenv can display specific environment variables or all of them if no argument is given.
VARIABLE SCOPE
A variable defined simply as VAR=value is local to the current shell. It exists and can be used within that shell session, but it will not be inherited by any commands or scripts executed from it. export VAR=value not only defines the variable but also marks it for inheritance by child processes. It's common practice to use export in shell initialization files (e.g., .bashrc, .profile, .zshrc) to set up the environment for interactive sessions and scripts.
FUNCTIONS AND EXPORT
While less common than exporting variables, export -f allows shell functions to be inherited by child shell processes. This is useful when a child shell needs to execute a specific function defined in the parent shell's environment, though it's important to note that functions are typically only exported to child shell instances, not to arbitrary executables.
HISTORY
The export command is a fundamental feature of the Bourne shell (sh) and has been inherited by its successors, including Bash (bash), Zsh (zsh), and Ksh (ksh). Its core functionality—making variables available to child processes—has remained consistent across these shells, reflecting its importance in Unix-like operating systems. The introduction of options like -f for functions and -p for listing exports has evolved with the shells, enhancing its utility over time and solidifying its role in shell scripting and environment management.