Export Command in Linux

Filed Under: UNIX/Linux

In this guide, we will look at the export command in Linux. Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes. Basically, a variable will be included in child process environments without affecting other environments. To get a clearer picture of what we are talking about, let’s dive in and have a look at the export command examples.

Export command in Linux without any arguments

Without any arguments, the command will generate or display all exported variables. Below is an example of the expected output.

$ export

Sample output

Export Command in Linux without any arguments

Viewing all exported variables on current shell

If you wish to view all exported variables on the current shell, use the -p flag as shown in the example

$ export -p 

Sample output

Export -p view all exported variables in current shell

Using export with functions

Suppose you have a function and you wish to export it, how do you go about it? In this case , the -f flag is used. In this example, we are exporting the function name ().

First, call the function

$ name () { echo "Hello world"; }

Then export it using the -f flag

$ export -f name

Next, invoke bash shell

$ bash

Finally, call the function

$ name


Hello World

export a function using -f

You can also assign a value before exporting a function as shown

$ export name[=value]

For example, you can define a variable before exporting it as shown

$ student=Divya

In the above example, the variable ‘student’ has been assigned the value ‘Divya’

To export the variable run

$ export students

You can use the printenv command to verify the contents of the variable as shown

$ printenv students

Check the output below of the commands we have just executed


exporting a variable

The above can be achieved in 2 simple steps by declaring and exporting the variable in one line as shown

$ export student=Divya

To display the variable run

$ printenv student


Export Command In A Few Steps

This concludes our tutorial about export command. Go ahead and give it a try and see the magic! Your feedback is most welcome.


  1. Rahul Bhichher says:

    Thank you so much Pankaj, it will help a lot.

  2. GSK says:

    Good info.
    I have a command where it says “export MY_KERNEL =/root/xyz/Linux-v4.12/” and then compiling a make file by command “make KDIR=$MY_KERNEL -j8”. what does it mean. I am able to compile the make file using just “make” command only. what is the difference in compiling make file with normal “make” and “make KDIR=$MY_KERNEL -j8”. Please help me

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content