Saturday, May 25, 2024
HomeTutorialsHow to Use the Linux Export Command in Everyday Computing

How to Use the Linux Export Command in Everyday Computing

The Linux export command marks which values need to be passed to a set of child processes. It is a simple but useful feature provided by the bash shell. It allows admins to pass configuration parameters for the environment without disrupting the current session. This is why the exported variables are not used until the terminal session has been restarted. Luckily, the export command is very simple to use and easy to master. In this guide, we will advise starting users on how to use export in Linux.

How to Utilize the Linux Export Command

You can make use of export in numerous ways. One common example of this is the use of export for setting up the user environment. Users can simply specify the variables using export and add them to their .profile file. Thus, the environment will be configured that way each time they log in.

1. Display all Exported Variables

When used without any argument, the export command displays a list of all of the exported variables for your environment. You should be able to view the variable’s names and their corresponding values.

$ export

It’s easy to find out information on specific variables by using the Linux grep command alongside export. The below commands illustrates this using a simple example.

$ export TEST="for testing purposes"
$ export | grep -i test

linux export command

2. Display Exported Variables for Current Shell

The -p flag of export prints out a list of all the exported variables for the current Linux shell. Check out the below example to see what do we mean by this.

$ export -p

You can use this command to troubleshoot various configuration issues for the running shell session.

3. Export Variables in Linux

The export command makes it easy to share variables across environments. You can set the value of the variable using an export statement. The below example demonstrates this.

$ export EDITOR=/usr/bin/gedit

This will set the path of gedit as the value of the EDITOR variable. You can confirm this by using grep.

$ export | grep -i EDITOR

4. Export Functions in Linux

Developers can use the -f option export for exporting functions. The following example demonstrates this using simple test functions. You can use this method for writing custom shell scripts.

$ test () { echo "Test Function"; }
$ export -f test
$ bash
$ test

This should display the string “Test Function” in your terminal window. The bash call was used to fork a child process for a bash. Without this, the test function would not print the text.

exporting function in bash shell

5. Configure Environment Properties

You can use the export command in Linux to configure various environmental parameters. For example, if you add the below line to your .bashrc file, it will set it as the path for Snap each time the system reboots.

$ echo export PATH="/snap/bin/lxd:$PATH" >> .bashrc

Do not worry if you’re not familiar with how this works from inside profile files. Simply add the custom exports at the end of the file. This way, you can always find and remove them if you want to.

Ending Thoughts

The Linux export command is a useful tool for configuring environment parameters. Moreover, it is very easy to master since there are only a few different options for this command. We have outlined some examples to help you better understand this tool. Hopefully, you can start to use export for customizing your environment from now on. Let us know your thoughts regarding this guide. Feel free to ask questions regarding export or any other Linux terminal commands below in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.


Please enter your comment!
Please enter your name here

You May Like It!

Trending Now