Linux export Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux export command and its practical applications. The lab covers understanding the purpose of the export command, setting environment variables using it, and examining real-world examples. We will learn how to create and manage environment variables that can be accessed by the current shell and any child processes, which is useful for configuring the environment for applications and scripts.

The lab provides a step-by-step guide, starting with an introduction to the export command and its role in working with environment variables. We then delve into practical examples, demonstrating how to set environment variables and verify their existence. The content covers both basic and more advanced use cases of the export command, ensuring a comprehensive understanding of this essential Linux tool.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/UserandGroupManagementGroup -.-> linux/env("`Environment Managing`") linux/UserandGroupManagementGroup -.-> linux/export("`Variable Exporting`") subgraph Lab Skills linux/echo -.-> lab-422670{{"`Linux export Command with Practical Examples`"}} linux/env -.-> lab-422670{{"`Linux export Command with Practical Examples`"}} linux/export -.-> lab-422670{{"`Linux export Command with Practical Examples`"}} end

Understand the Purpose of the export Command

In this step, we will explore the purpose of the export command in Linux. The export command is used to set environment variables that can be accessed by the current shell and any child processes (such as programs or scripts) that are launched from the current shell.

Environment variables are named values that are stored in the shell's environment. They can be used to store configuration settings, paths, or any other information that needs to be accessible to the shell and its child processes.

Let's start by checking the current environment variables in our Ubuntu 22.04 Docker container:

env

Example output:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG=C.UTF-8
LANGUAGE=en_US:en
LC_ALL=C.UTF-8
HOME=/home/labex

As you can see, there are several environment variables already defined, such as PATH, LANG, and HOME.

Now, let's create a new environment variable using the export command:

export MY_VARIABLE="Hello, World!"

We can verify that the variable has been set by using the env command again:

env | grep MY_VARIABLE

Example output:

MY_VARIABLE=Hello, World!

The export command makes the MY_VARIABLE environment variable available to the current shell and any child processes that are launched from it. This can be useful for configuring the environment for your applications or scripts.

In the next step, we will explore more practical examples of using the export command.

Set Environment Variables Using the export Command

In this step, we will learn how to set environment variables using the export command and explore some practical examples.

First, let's create a new environment variable:

export MY_APP_HOME="/home/labex/myapp"

We can verify that the variable has been set:

echo $MY_APP_HOME

Example output:

/home/labex/myapp

Environment variables set using export are accessible within the current shell session. However, if you open a new shell or terminal, the variable will not be available. To make the variable persist, you can add the export command to your shell's startup script, such as ~/.bashrc or ~/.zshrc.

Let's add the MY_APP_HOME variable to the ~/.bashrc file:

echo 'export MY_APP_HOME="/home/labex/myapp"' >> ~/.bashrc

Now, the MY_APP_HOME variable will be available in all new shell sessions.

You can also use the export command to set multiple environment variables at once:

export MY_APP_VERSION="1.2.3" MY_APP_CONFIG="/etc/myapp.conf"

This will set both MY_APP_VERSION and MY_APP_CONFIG environment variables.

Environment variables set using export can be used in your scripts and applications. For example, you can use the $MY_APP_HOME variable in a script to reference the application's home directory:

cd $MY_APP_HOME

In the next step, we will explore more practical examples of using the export command.

Explore Practical Examples of the export Command

In this final step, we will explore some practical examples of using the export command.

One common use case for export is to set environment variables for your applications or scripts. For example, let's say you have a Python script that needs to access a database. You can set the database connection details as environment variables and use them in your script:

export DB_HOST="mydb.example.com"
export DB_USER="myuser"
export DB_PASS="mypassword"
export DB_NAME="mydbname"

Then, in your Python script, you can access these variables:

import os

db_host = os.getenv("DB_HOST")
db_user = os.getenv("DB_USER")
db_pass = os.getenv("DB_PASS")
db_name = os.getenv("DB_NAME")

## Use the environment variables to connect to the database

Another example is setting the PATH environment variable to include custom directories. This can be useful if you have installed software in a non-standard location and need to make it accessible to your shell:

export PATH=$PATH:/opt/myapp/bin

Now, any commands located in the /opt/myapp/bin directory can be executed without specifying the full path.

You can also use export to set environment variables for specific commands or scripts. For example, if you have a script that needs to run with a specific PYTHONPATH setting, you can set it using export before running the script:

export PYTHONPATH=/path/to/my/python/modules
python my_script.py

This ensures that the PYTHONPATH environment variable is set correctly for the duration of the script execution.

Remember, environment variables set using export are only available within the current shell session. If you want to make them persistent, you should add the export commands to your shell's startup script (e.g., ~/.bashrc or ~/.zshrc).

Summary

In this lab, we learned the purpose of the export command in Linux, which is used to set environment variables that can be accessed by the current shell and any child processes. We started by checking the existing environment variables and then created a new environment variable using the export command. We also explored how to set environment variables and access them using the echo command. The export command makes the environment variables available to the current shell and any child processes, which can be useful for configuring the environment for applications or scripts.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like