Oct 26, 2023 3 min read

Export Command on Linux

Use export command on Linux with our step-by-step tutorial. The export command on Linux is used to set environment variables.

Export Command on Linux
Table of Contents

Introduction

The export command on Linux is used to set environment variables. Environment variables are variables that contain information about the system or user preferences. These variables are accessible by various programs and scripts running on the Linux system.

When used with the export command, a variable is made available to other processes spawned from the current shell session. This means that any child processes can access and utilize the environment variable.

In this tutorial, you will understand export command on Linux. We will also address a few FAQs on Export Command on Linux.

Export command on Linux without any arguments

The command will create or display every exported variable if it is given no arguments. An example of the expected output is provided below.

export

Sample output

Export Command in Linux without any arguments

Viewing all exported variables on current shell

Use the -p flag as demonstrated in the example to display all exported variables on the current shell.

export -p 

Sample output

Export -p view all exported variables in current shell

Using export with functions

How do you export a function if you have one? The -f flag is used in this instance. In this example, the function name () is being exported. Call the function first.

name () { echo "Hello world"; }

Use the -f flag to export it after that.

export -f name

Call the function to finish.

name

Output

Output
Hello World

As seen, you may also assign a value before exporting a function.

export name[=value]

You may, for instance, define a variable first before exporting it as displayed.

student=Divya

The value 'Divya' has been assigned to the variable 'student' in the example above. Run the variable export.

export student

The printenv command can be used to check the value of the variable as displayed.

printenv student

Verify the commands we just executed's Output by looking at it below.

The above can be accomplished in two simple steps by declaring and exporting the variable in a single line, as shown.

export student=Divya

Run the following command to display the variable:

 printenv student

Output:

FAQs on Export Command on Linux

How do I use the "export" command to set an environment variable? 

To set an environment variable using the "export" command, use the following syntax: export VARIABLE_NAME=variable_value. Replace "VARIABLE_NAME" with the desired name of the variable and "variable_value" with the value you want to assign.

How can I view the current environment variables in Linux? 

You can view the current environment variables in Linux by executing the command printenv or env. This will display a list of all environment variables and their values.

Can I export multiple variables in a single command? 

Yes, you can export multiple variables in a single "export" command by separating them with spaces. For example: export VAR1=value1 VAR2=value2.

How can I make an exported variable persist after reboot? 

To make an exported variable persist after reboot, you need to add the "export" command to an initialization file such as .bashrc or .bash_profile. Open the file in a text editor, add the "export" command, and save the changes.

Can I remove an exported variable using the "export" command? 

No, the "export" command does not remove an exported variable. To unset a variable, you would use the unset command. For instance: unset VARIABLE_NAME.

How can I check if an environment variable is exported? 

To check if an environment variable is exported, you can use the env command along with a pipe to search for the variable. For instance: env | grep VARIABLE_NAME.

Does exporting a variable in a shell affect subshells? 

Yes, when you export a variable, it becomes accessible to subshells or child processes of the current shell session.

Conclusion

This brings us to the end of our export command tutorial. Give it a shot and see what happens! Your comments are much appreciated.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.