Linux Commands

Linux Export Command with Examples

The export command is a built-in command of the shell. Environmental variables are exported in it as child processes. The existing environment variables are not affected. It is also known as the bash bullets command. The export command helps update the current position of the shell session because of the change we have made with the exported variables. No wait is required for the new shell session to be started. Export variables are defined as POSIX because the shell provides the export attribute to the variable with the specific name. Suppose the name of a variable is set and followed by “=any word,” then the variable is assigned with the value of that word. There are two paths used to export and are present in the environment. According to the required demand, the path of the variables is exported globally or at the session-level.

  1. A Global path
  2. Session level path

In this article, we will discuss different functions of the Export command.

Prerequisite

To understand the concept of the Export command in Linux, you need to have a Linux environment running on your system. This can be possible by having a virtual machine on your system to create a Linux process virtually. Firstly, download a Ubuntu file from the internet. After installation, configure it on the Virtual box. After successful configuration, you will see that Linux Ubuntu is running on your system. One should mention and create a user in the starting configuration because it will be needed to access the applications.

Syntax

Export [-f -n] [name [= value] …... ]
Export -p

The Export keyword is used in the command as it is considered in two different ways. Either written with the command, arguments or written with the path.

  • -f is used to display the list of all the names that we export by using the command in the environment of the live shell.
  • -n helps in removing the names from the export list.
  • -p depicts the use of exporting the names that will act as a function.

The Export command having no argument

It is used to display all the variables that are exported in the environment by your system. We will execute the below command.

$ Export

The values that are obtained are available on all the shells globally.

Working of EXPORT command in Linux

This command is used to export the path of shell variables or the path of all applications in the system. As discussed above that, the path can be at the global or session-level. Globally, presence is the one that is available either the server or the shell is in reboot condition. At the same time, the login session is only available in the current session. There will be no scope of a session if the server is rebooting. The software path can also be exported.

To use the export command in your system, there is no need to install any package or any repository in your system.

Features of Export (options)

Now moving forward, we will now see the possible options of the Export command.

-p is used for enlisting all the names that are being used in the current shell. -n helps in removing the names from the exported list. -f is to export the names as functions.

We will see these options working one by one in the article.

The export command with arguments

1. –p

$ export -p

From the output, you can see that all the variables are exported used in the current shell.

2. –f

To export a function, you first need to write a function in your command line with the unique function name. So that we can call the function easily. Once the function is exported, we can easily access it by calling the name of the function. Consider the following code.

$ Name () {echo “Ubuntu”;}

$ Export –f name

$ bash

$ Name

Here we have used name () as the function. In this function, we have just printed the name. After defining the function, we will export the function with the help of the “-f” keyword. Bash function is also exported here. The usage of the bash function is to open a new child shell to precede the function forward. Then type the name of the function. The output will be:

3. -n

This feature is used to remove the variable. In this part, we are going to remove “EDITOR.”

$ export –n EDITOR

$ export | grep EDITOR

After applying the first command, you will see that no output is obtained, so for confirmation, we will use the grep command to export the variables if they are present.

Value assigning before exporting a function

The export command allows us to introduce the value assigning before exporting a function.

$ x = 10

$ export x

$ printenv x

X is a variable to which we have assigned a value. Then after exporting, we have displayed the output by using “printenv” to print the value of the x variable.

Consider another example of exporting a function after assigning the value. Here a variable Linux is assigned with a string. The variable is exported, and then we will display its output.

Export multiple variables

To export multiple variables, consider an example in which we have initialize three variables with the values. These values are then exported in a sequence. Finally, we take print of the values in the variables.

$ Export x=1

$ Export y=2

$ Export z=5

$ Export x y z

$ printenv x y z

From this, we can easily obtain the output.

As a text editor, set vim

Use the below-cited command for this purpose.

$ export EDITOR= /usr/bin/vim

$ export | grep EDITOR

No output will be obtained after exporting. Then we will grep the variables to obtain the output.

Colorful prompt

We can color the prompt by using the following command in the shell. This will change the color of the prompt to green.

$ export PS1 = '\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '

You can see the output that its color is changed to green.

Name value exporting

Exporting name value in the Linux environment is very common. When we export the name value, the scope level of the name is restricted to the login shell. When the command-line or shell session is finished, then the name-value will also no longer exist.

$ export JAVA_HOME=/usr/share/java-1.8.0/

Here we have exported the path of the JAVA_HOME

$ Echo $JAVA_HOME

For verification, we will echo the term.

Conclusion

This article explains the working and usage of export commands in Linux. It is a built-in feature that helps initiate the variables, export name values, and delete the variables.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.