Updated May 29, 2023
Introduction to Linux Export
In the Linux ecosystem, the export command is used to export the path on the server level. It is mainly used for the shell or bash jobs on the Linux level. It is a very vital way to pass the variables on the shell jobs. The export command is also called as the BASH BUILTIN COMMANDS.
Below is the two different export path or variables present in the environment.
- Global Path
- Session level path.
The Export command utility was written by Jim Meyering.
Syntax of Export Command
export [-f -n] [name[ = value] ... ]
or
export -p
- export: We can use the export keyword in the syntax or command. It will work in two different ways i.e. command with arguments and with the export path. As per the requirement, the export command will export the path on a global level or session level.
- -f: It will display the list of all names that are export in the live shell environment.
- -n: It will remove the names form the export list.
- -p: It will export the name as a function.
How Does Linux Export Command Work?
The export command is basically used to export the path of the shell variables or to export the path application path on the Linux environment.
The export path is global or the login session-level i.e. the global variable/path will be available after the session or server will reboot but the login session path or variable scope will only be available with the current session only. If the server or session will reboot, the scope of the session will no longer work.
When we are exporting the path, the path will be accessible for the shell jobs or the application jobs as well. We can also export the software path also. The same export software path will be accessible by downstream applications or software. There is no need to install any separate packages for them they can use or access the export software path.
Examples to Implement Linux Export Command
Below are the examples of Linux Export Command:
Example #1 – To Export Name Value
It is a very simple and common way to use the export name value in a Linux environment. When we will export the name value on Linux, the scope of the name-value is limited to the login shell only. If the shell session will vanish then the scope of the name-value will also vanish.
Command:
export JAVA_HOME=/usr/share/java-1.8.0/
Explanation: As per the above export command, we are exporting the JAVA_HOME path on the Linux environment. But by default, the path is not exported if the java was manually installed by the end-user (refer to screenshot 1 (a)). We need to export the JAVA_HOME path via the export command (refer to screenshot 1 (b)). But the export name value is temporary; it is not permanent in the environment. Once the session will be terminated the export name-value vanishes.
Command:
echo $JAVA_HOME
Output:
Command:
export JAVA_HOME=/usr/share/java-1.8.0/
echo $JAVA_HOME
Output:
Example #2 – To Export Function
In the export command, we are having the functionality to export the shell terminal function. We need to use the special “-f” flag with the export command.
Command:
export -f exp_funtion
Explanation: With the help of export command, we are able to export the function on the shell prompt. As per the below command, we are exporting the “exp_funtion” function. Before exporting the function, we are getting the “command not found error” (refer screenshot 2 (a)). To export the function, first, we need to write the function in the shell with specific function name (we have used “exp_funtion” function name). In the export command, we need to use the “-f” option with the command. Once the function will export, we can access the function with their name in the shell jobs or as a normal command (refer screenshot 2 (b)).
Command:
exp_function
Output:
Code:
exp_function () { echo “Hello EDUCBA”; }
export -f exp_funtion
exp_function
Output:
Example #3 – Remove Name from Exported List
In export command, we are able to remove the list of export name form the exported list. To remove the name value from the exported list, we need to use the “-n” option with the export command.
Command:
export -n LogPath
Explanation: As per the above export command, we are able to delete the export entry from the export list as we all shell environment. As per screenshot 3 (a), we are listing the list of export values. Then we are adding the export value in the Linux environment “LogPath” (refer screenshot 3 (b) & (c)). After that, we are removing the same export value from the Linux environment (refer screenshot 3 (d)).
Command:
export -p
Output:
Command:
export LogPath=" /var/log"
echo $LogPath
Output:
Command:
export -p
Output:
Command:
export -n LogPath
export -p
Output:
Example #4
In Export command, we are able to print all the export values in the environment. It will display all the global export values of the Linux environment.
Command:
export
Explanation: As per the above command, we will get the export values for the Linux level. These values are globally available in all the shells.
Command:
export
Output:
Example #5 – Print Export Value for Login Shell
In the export command, we are having the functionality to print the export values only available for the login shell. It is not mandatory that the export values that are visible in the shell window are valid globally.
To print the current export values for the login shell. We need to use the “-p” option with the export command.
Command:
Export -p
Explanation: As per the above command, we will get the export values for the current login shell.
export -p
Output:
Conclusion
We have seen the uncut concept of “Linux Export Command” with the proper example, explanation and command with different outputs. The export command can define or export the paths or values on a global or shell level. It is useful to run the application or shell-level jobs.
Recommended Article
We hope that this EDUCBA information on “Linux Export” was beneficial to you. You can view EDUCBA’s recommended articles for more information.