The Linux Export command is one of the many built-in commands available in the bash shell. It is a pretty straightforward command that only takes three arguments, which we will discuss below. In general, the Export command marks functions and environmental variables to be passed to newly created child processes.
Therefore, the child process inherits all marked variables. If this sounds all-new, read on as we will discuss this process in more detail.
Linux Export command arguments
The general syntax of the Export command is:
export [-f] [-n] [name[=value] ...] or export -p
- -p
Lists all currently exported variables on current shell - -n
Remove names from the export list - -f
Exports names as functions
Understanding the Export command
To get started, let’s look at the commands below.
$ x=FOSSLINUX.COM $ echo $x FOSSLINUX.COM $ bash $ echo $x $
- Line 1: We declare a new variable ‘x’ and pass the string, “FOSSLINUX.COM.”
- Line 2: With the echo command, we display the contents of the variable ‘x.’
- Line 3: We create a new child bash shell.
- Line 4: We use the echo command to display the contents of the variable ‘x’ in this child process. However, we get an empty value.
From the example above, we see that a child process does not inherit variables from the parent process. After we created a new child process with the bash command, we could no longer access the contents of variable ‘x’ from this child process. Now, this is where the Export command comes in handy.
Let’s look at a new version of the code above now using the export command.
$ x=FOSSLINUX.COM $ echo $x FOSSLINUX.COM $ export $x $ bash $ echo $x FOSSLINUX.COM
From the example above, we exported the variable ‘x’ using the export command on line three. After creating a new child process with the bash command, we were still able to access the contents of variable ‘x.’
Export command examples
Now that we have a good understanding of the Linux export command, we can look at some technical examples.
Example 1: View exported variables
Here is how to view all the currently exported environmental variables of your system with the export command.
$ export
Consider the below snap of output.
From the sample output above, we can see all the passed Environmental variables in our system. Some of the easily notable include the Hostname, the Home directory, the currently logged in user, the current Desktop Environment, etc.
Example 2: List all variables
Use the export command with the ‘-p’ argument to list all variables exported in the current shell.
$ export -p
Consider the below snap of output.
From the output above, you will notice most of the environmental variables are similar to those in our previous example (export). That is even after creating a new shell. Therefore, the child shell has inherited all the marked environmental variables from the parent. Variables like Hostname, Home directory, and Log-name have the same values as the parent.
Example 3: Exporting a shell function with the ‘-f’ argument.
Let’s create a simple bash function that will display the name ‘FOSSLINUX.COM’ when called.
$displayWebsite () { echo "FOSSLINUX.COM"; } $displayWebsite FOSSLINUX.COM $export -f displayWebsite $bash $displayWebsite FOSSLINUX.COM
- Line 1: We created a function called ‘displayWebsite,‘ which would echo the name “FOSSLINUX.COM” when called.
- Line 2: We called the ‘displayWebsite‘ function.
- Line 3: We export our ‘displayWebsite‘ function.
- Line 4: We created a new child bash shell.
- Line 5: We called the ‘displayWebsite‘ function again, in our new child shell.
4. Example 4: Remove an exported variable from the export list.
To do so, we will use the ‘-n’ parameter. From the previous example, we have exported the ‘displayWebsite’ function. We can verify this using the ‘grep’ command below.
export | grep displayWebsite
To remove the ‘displayWebsite’ function from the list, we will execute the command below.
export -n displayWebsite
To confirm that, we will run the ‘grep’ command again.
Example 5: Set a text editor
We can also use the export command to set ‘nano’ as our text editor. We will use the grep command to verify our export.
$ export EDITOR=/usr/bin/vim $ export | grep EDITOR
Example 6: Change Color
Set a fantastic colorful terminal prompt with the export command.
Execute the command below to change the color of your prompt to green.
export PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
Generally, the variable PS1 holds the bash prompt. You can always change the values to customize it to your liking. The command above will not display any output but will only change the color code of your prompt to green
Conclusion
That concludes our article on the Linux Export command. Those are only some of the basic examples you can use with this command. You can also look at the export man page for more information with the command below.
$ man export