How to use export command in Linux


The export command is a built-in command of the bash shell. It is used to mark the environment variables and functions exported to the child processes.

Environment variables are set when you start a new shell session. At any time if you change the value of any of these variables, the shell has no way to select that change automatically until a new shell session is started. The export command provides the ability to update the current shell session about the changes made by you to the exported variable. So you don’t need to wait for the start of a new shell session.

In this article, you will see the usage of the export command in Linux along with some examples.

The syntax of the export command

The syntax of how to use the export command in Linux is given below.

export [-f] [-n] [name[=value] ...]

OR

export -p

Where name and value are the names of the environment variable and their values.

There three options that you can use with this command –

-p – List all the names that are exported in the current shell

-n – Remove names from the current list

-f – Names are exported as functions

Using export command without any argument

When you execute the export command without any argument it will display the list of all environment variables on your system.

export

You can see the output of this command in the given image.

Display all exported variables in the current shell

You can display all the exported variables in the current shell by using the option -p with the export command.

For example –

export -p

This will display the given output.

How to use export with a function

If you want to export a function instead of a variable you should use option -f with the export command.

In the given example we are exporting the function name().

First, define the function.

name()  { echo "cyanogenmods.org"; }

Now export the above function by using –

export -f name

Next invoke the bash shell to execute this function.

bash

Finally, call the function

name

You can see this in the given image –

Assign a value before exporting a variable or function

You can assign a value to a variable or function before exporting it.

For example –

Assign a value to a variable

x=15

And then export it –

export x

You can check the value of x by using –

printenv x

How to make an application open as default

You can set an application default using the export command for example to set vim as your default text editor you can use the given command in your terminal –

export EDITOR=/usr/bin/vim

And the use –

export | grep EDITOR

The command will not show any confirmation.

If you want you can read how to set environment variables in Linux.

Conclusion

Here you have learned about the export command in Linux. Now if you have any query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.