Export command in Linux | Explained

Export is the built-in command of the bash shell, when the environmental variables are created in the bash shell they do not inherit the changes of their parent shell which means when the new shell will be opened the values of the parent shell will not be exported to the new child shell. To export the values of the environmental variables in the child shell, we use the export command to export these values to the child processes.

In this follow-up, we will explain the utilization of the export command in Linux.

What is the export command in Linux

The export command is used to mark the environmental variables so these can be exported to the child shells. The general syntax of using the export command:

$ export [options] [variable-name]

We can use two arguments with the export command; one can be any option (will discuss these options later) and variable name which is to be marked for exporting in the child shell.

Most frequently the following options are used with the export command:

OptionsExplanation
-pIt displays the exported variables of the current shell
-nIt is used to remove the variable from the list of exported variables
-fIt is used to export the functions from the parent to child shell

To understand the usage of the export command, we will consider the basic level example first. We will declare a variable “myVariable” and store a string of “LinuxFoss” in it using the command:

$ myVariable=LinuxFoss

To display the store value of the “myVariable”, use the echo command:

$ echo $myVariable

The value “Linux Foss” has been displayed, open the child shell by running the bash command:

$ bash

The new bash shell has been opened, print the value of “myVariable” created in the parent shell:

$ echo $myVariable

Nothing has been displayed in the output because the “myVariable” created in the parent shell is not exported to the child shell, so we will exit the child shell:

$ exit

Once we are in the parent shell, we will export the “myVariable” to the child shells by using the export command:

$ export myVariable

Again, open the child shell:

$ bash

Print the value of “myVariable” using the echo command:

$ echo $myVariable

In this way, we can mark the environmental variables and export them to the newly created shells or child shells. Now, if we want to display the export variables of the current shell, we can use the “-p” flag:

$ export -p

We can see in the last of the list the name of “myVariable” which we exported in the previous commands:

And if we want to remove “myVariable” from the export variable lists, we can use the “-n” option:

$ export -n myVariable

If we again display the list of export variables of the current shell using the “-p” flag:

$ export -p

We will not see the name of “myVariable” in the entire list:

The name has been removed, similarly, we can create a function, myVariable() in the parent shell and store some value in it:

$ myVariable() { echo “HELLO! Its LinuxFoss”; }

To export this function to the child shell, we will use the “-f” flag otherwise it will consider it a name of the variable, not a function:

$ export -f myVariable

Open the new shell using the “bash” command and print the value of myVariable():

$ myVariable

If the “export” command is executed without using any options, it will display the list of all exported variables and functions:

$ export

The export command is also used for setting the vim editor as a default editor of Linux, to set the Vim editor to your default editor using the export command:

$ export EDITOR=/usr/bin/vim

To verify the vim editor has been set as the default editor, use the grep command:

$ export | grep EDITOR 

Conclusion

The export command is one of the built-in commands of the bash shell which is used to export the environment variables from the parent shell to the newly created child shell. In this follow-up, the export command is explained with its utilization in Linux with the help of some examples. We also explained the options used along with the export command.