The Linux export command is used when you want to export an environmental variable as a child process. Doing so ensures the existing environment variables don’t get affected. This built-in shell command helps with globalizing variables to make them accessible even when you open a new shell session.
The export command in Linux takes three main options. The -f is included when you want to export a function. The -n is added when you want to remove the specified name from the exported variables. Lastly, the -p helps with displaying all the exported variables in the current shell. To make it clearer, let’s discuss a few examples of working with the export command in Linux.
Examples of Working with the Export Command in Linux
Anyone can use the export command provided you understand its various functionalities. Here are a few examples.
Example 1: Basic Usage
When you run the export command without any argument, it will give an output showing all the exported variables.
Example 2: Working with the -p Option
The first example gives an output showing all the exported variables, even from other shell sessions. However, if you only want to see an output for the current shell, add the -p option when running the command.
Go through the output and compare it with the one from the first example; you will notice that some results will have been omitted for this output.
Example 3: Working with -f
With the export command, you can also export functions that you can call to perform a given task. Adding the -f option indicates that you are exporting a function and not a variable.
In the below example, we’ve created the greetings () that prints a welcome message. We then exported it using the export -f command.
Before we can call the exported function, we must first invoke the bash shell. Afterward, call the function by typing its name. Its output will be what you specified in the function definition.
Example 4: Assigning Value to a Variable Before Exporting It
Still, with the export command, it’s possible to define and initialize a variable and then export it. For instance, let’s define the ‘username’ variable and initialize it using the equal sign. Once we have our variable, we can then export it by specifying it’s name as follows.
To access the variable’s value, we can use the echo command or run the printenv command and specify the variable’s name.
The output will be the value of your variable. This example is ideal when you want to store the value of a variable and use it with other commands. You can reference the variable when performing other tasks on your command-line and if working on automation, knowing how to export such a variable is handy.
Example 5: Exporting Multiple Variables
So far, we’ve only worked with a case where we are exporting one variable. However, it’s possible to export several variables simultaneously.
The first step is to declare and initialize the variables separately. You can then run the export command and specify the variable names separated with spaces, as shown below.
After exporting the variables, you can access them using the echo or printenv command. Again, you can specify to get the output of one variable at a go or display all the values with a single command.
Example 6: Filtering the Exported Variables
Our first example demonstrated how to view all the exported variables by running the export command. When you want to get more specific results, you can add a filter such as using the grep command. For instance, let’s filter the output to only show where ‘name’ is found.
Our output now shows fewer results.
Example 7: Deleting Exported Variables
Any exported command can be deleted by adding the -n option when running the export command. Or instance, to delete the ‘name’ ‘nd username variables, run the below commands.
If we try to filter the results, we get no output as the variables have already been deleted.
Conclusion
The export command in Linux is used when you want to view and work with exported environment variables. This post shares examples of working with the export command to achieve different functionalities. Hopefully, the examples are enough to help you understand the export command in Linux.