Introduction
export
command in Linux marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables. Frequently Used Options
- -p : List of all names that are exported in the current shell
- -n : Remove names from export list
- -f : Names are exported as functions
Exported variables such as $HOME
and $PATH
are available to other programs run by the shell that exports them as environment variables. Regular (non-exported) variables are not available to other programs.
Example
Variable va is not defined and exported so grep will not show any result.
env | grep 'va=' # No environment variable called variable va=Hello # Create local (non-exported) variable with value env | grep 'va=' # Still no environment variable called variable
After exporting the variable using export command, it is available from the child process.
export va # Mark variable for export to child processes env | grep 'va=' # Output is 'va=Hello'
To remove this variable from the export list we need to use the -n export option.
export -n va
The most common use of the export command is when defining the PATH
shell variable. In the example below, we have included additional path /usr/local/bin to the existing PATH definition.
export PATH=$PATH:/usr/local/bin
Exporting Shell function
With the option -f the export
command can also be used to export functions. In the example below, we will create a function called print()
and export it.
$ print () { echo "Hello"; } # Define function $ export -f printname # Export function $ bash # Bash shell $ print # Output is 'Hello'
print function is accessible in the new bash shell created using bash
command.