export command in Linux with examples

The export command in Linux is used to set an environment variable. Environment variables are part of the Linux system shell that contain changing values. They help facilitate scripts and system programs, so that code can accommodate a variety of scenarios. Unlike regular shell variables, environment variables can be accessed system-wide, by any user or process.

There are a couple different ways to use this command, depending on if you want to set a temporary environment variable or set one permanently. Perhaps one of the most common uses of the export command is to add a new directory to the path environment variable. Regardless of what you want to do with the command, we’ll show you everything it’s capable of in this tutorial.

In this tutorial, you’ll learn how to use the export command in Linux through examples. Follow along below to see learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the export command on Linux
export command in Linux with examples
export command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software export
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

You don’t ordinarily need to use any extra options with the export command. In the examples below, we’ll show you some of the most common ways to use this command. This will involve setting a temporary environment variable, a permanent environment variable, and adding a directory to the path environment variable in Linux.

export command in Linux Basic Examples

  1. Use the following command to create a new shell variable. This will only make the variable active in your current session, but we will make an environment variable soon.
    MY_SITE='linuxconfig.org'
    

    Next, use the export command to set the new variable as an environment variable.

    $ export MY_SITE
    
  2. Alternatively, we can set the temporary environment variable by using a single export command with this syntax:
    $ export MY_SITE="linuxconfig.org"
    

Note that setting a temporary environment variable, such as in the examples above, will be erased whenever the system reboots.

export command in Linux – add directory to path examples

  1. To add a directory to $PATH for the current session, use the following command syntax. In this example, we’re adding the /bin/myscripts directory.
    $ export PATH="/bin/myscripts:$PATH"
    

    You can verify afterwards that the directory has been added.

    $ echo $PATH
    /bin/myscripts [...]
    

    Now, files we have stored in the /bin/myscripts directory can be executed anywhere, without specifying their full path. This configuration will change when we end the current session (reboot the PC or close the terminal). To make it permanent, check out the example below.

  2. To add a directory to $PATH permanently, we’ll need to edit the .bashrc file of the user you want to change. Use nano or your favorite text editor to open the file, stored in the home directory.
    $ nano ~/.bashrc
    

    At the end of this file, put your new directory that you wish to permanently add to $PATH.

    export PATH="/bin/myscripts:$PATH"
    

    Save your changes and exit the file. Afterwards, execute the following command to make the changes take effect in your current session. Alternatively, you can log out or reboot the system.

    $ source ~/.bashrc
    

    You can check $PATH once more to verify the change.

    $ echo $PATH
    
Adding a directory to $PATH on Linux
Adding a directory to $PATH on Linux
NOTE
You can always use the man command to read more about the export command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

The most advanced usage of the export command involves setting a permanent environment variable in Linux. Check out the examples below for a few different ways to do that.

export command in Linux Advanced Examples

  1. When setting a permanent environment variable, we aren’t actually using the export command on the command line. Instead, we will add an export command into a configuration file. In this first method, we will edit the ~/.bashrc file. Variables stored here will reside in the user’s home directory and are only accessible by that user. The variables get loaded any time a new shell is opened.

    Add a new variable to the ~/.bashrc configuration file by appending a line to the end of it with this syntax. Notice we precede each new variable with export.

    export MY_SITE='linuxconfig.org'
    

    Afterwards, you can load the new environment variables into the current session with the following command.

    $ source ~/.bashrc
    
  2. The second method to create a permanent environment variable is by editing the /etc/profile file. Variables stored here will be accessible by all users and are loaded whenever a new shell is opened. Add a variable to this file by using the same syntax as mentioned above.
    export MY_SITE='linuxconfig.org'
    

    Afterwards, you can load the new environment variables into the current session with the following command.

    # source /etc/profile
    
  3. This last method doesn’t actually involve the export command in any way. However, it would be remiss of us if we didn’t mention it since all of these examples are in the same vein. The third way to add a permanent environment variable in Linux is by editing the /etc/environment file. Variables stored here are accessible system-wide.

    If adding an environment variable to the /etc/environment file, you don’t need to precede the line with “export”.

    MY_SITE='linuxconfig.org'
    
Loading a new environment variable and testing it with the printenv command
Loading a new environment variable and testing it with the printenv command

Using the methods above, your variable configurations will persist until you delete them.

Closing Thoughts

In this tutorial, we learned all about the export command on Linux. The export command is used to set environment variables on Linux, either temporarily or permanently. You’ve now learned several different methods of using the export command for either purpose. Whether you need to add a new program to the PATH variable, or set a custom environment variable, the export command makes this rather easy.