Home Learn Linux Linux Export Command with Examples

Linux Export Command with Examples

by Arun Kumar
Linux Export command

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
$
Export Basics

Export Basics

  • 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
Export Command basics

Export Command basics

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.

Export command

Export command

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.

Export -p command

Export -p command

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
Export command

Export command

  • 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
Export command

Export command

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.

Export command

Export command

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
Export command

Export command

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\] '
Export command

Export command

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

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.