export Command in Linux
Overview
The export command in Linux is a built-in shell command used to export environment variables from the shell to child processes. It's an essential tool for configuring and managing the environment of your Linux system.
Syntax of export Command in Linux
The basic syntax of the export command in Linux is as follows:
Where:
- -nf: These are options that can be used with the export command. '-n' removes names from the export list, and '-f' exports function names.
- name=value: This is the name of the variable and the value you want to assign to it.
Options in export Command in Linux
-
-p: This option displays a list of all names that are exported in the current shell.
For example -
Output:
This command is useful to check the current exported environment variables and functions.
-
-n: This option removes the export property from each NAME.
For example -
Output:
This command removes VARIABLE from the list of exported variables.
-
-f: The -f option means the NAMES refer to functions.
For example -
Output:
This command exports a function called 'function_name'.
Example Usages
-
Setting a new environment variable:
Output:
Explanation: This command creates an environment variable called VARNAME and assigns the value "VALUE" to it.
-
Exporting a function:
Output:
Explanation: This command exports a shell function called 'function_name' so it can be used in child processes.
Tips
-
The export command in Linux affects only the current shell and its child processes, not the parent shell or sibling processes.
-
The -p option is useful to check your current environment.
Advanced Use Cases of export Command in Linux
-
Exporting multiple variables at once:
Output:
Explanation: This command sets and exports multiple environment variables at once.
-
Unsetting an exported function:
Output:
Explanation: This command removes the exported function 'function_name' from the list of exports, making it unavailable in child processes.
-
Exporting variables from a file:
Output:
Explanation: This command reads and executes commands from file_name in the current shell environment, effectively exporting variables defined in the file.
Conclusion
-
The export command in Linux is used to export environment variables from the shell to child processes.
-
The -p, -n, and -f options modify the behavior of the export command.
-
The export command only affects the current shell and its child processes.