Tutorial

Export Command in Linux

Published on August 3, 2022
author

Pankaj

Export Command in 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

Output

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 Output 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

Output 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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
March 14, 2021

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

- Rahul Bhichher

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 27, 2020

    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

    - GSK

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.