How to Set Environment Variables in Linux

Try this guide with our instant dedicated server for as low as 40 Euros

Bash Export

Have you ever felt your Linux terminal could use a little extra kick?

What if you could transform it into a personalized command center?

You can do these and more by working with environment variables that can supercharge the shell and applications.

The environment is an area that the shell builds every time it starts a session. It contains variables that define various system properties that can impact how programs run. 

In this tutorial, we will discuss how to export and use environment variables in Linux. 

But before that, let us have an overview of the environment variables and then go into the details of exporting and setting them.

Table Of Contents

  1. What are Environment Variables?
  2. Key Characteristics of Environment Variables
  3. Why Should You Export Environment Variables?
  4. How to Set a Variable in Bash?
  5. How to Export a Variable in Bash
  6. How to Export Bash Functions
  7. List All Exported Variables
  8. Conclusion
  9. FAQs

What are Environment Variables?

Environment variables in Linux are a collection of key-value pairs that influence how applications and the shell itself behave within the system. It stores information about the system environment, such as the location of executables, user-specific settings, and system configuration details. 

Key Characteristics of Environment Variables

The key characteristics of environment variables are as follows:


Environment variables can change overtime, allowing for flexible configurations that adapt to different requirements.


Child processes inherit environment variables from their parent process, ensuring consistency in settings and configurations.

Global or Local Declarations

Environment variables can be set globally, affecting all users and sessions, or locally, affecting only the current session or script.

Why Should You Export Environment Variables?

Exporting environment variables is a common practice in software development and system administration for several reasons. Some of them are:

Inter-process Communication

Environment variables facilitate sharing information between processes. By default, variables set in a shell session are only accessible within that session. Exporting a variable from a parent to a child process enables the child process to utilize the variable’s value, allowing for seamless communication and configuration sharing.

Configuration Management

Environment variables store configuration settings. By exporting the variable, applications are configured without hardcoding values into the codebase. 


Sensitive information such as API keys, and database credentials are stored in environment variables. This approach keeps these details out of the codebase and version control systems, reducing the risk of accidental exposure.


Environment variables facilitate the deployment of applications across different systems and environments. It ensures the application behaves consistently regardless of where it is deployed and the environment in the target system.


Environment variables provide a flexible way to alter application behavior without modifying the code. For instance, users can enable or disable debugging features or switch between different logging levels by changing environment variables.

By now you have an understanding of environment variables, and why the idea is important. Now, let us discuss how to set an environment variable.

How to Set a Variable in Bash?

Environment variables are a set of dynamic named values that influence the behavior of various processes running on the system. 

Creating a variable in Bash is straightforward. Simply assign a value to a variable name using the following syntax:


If a variable has more than one value, separate them with a semicolon:


For instance, consider the following statement to create a variable named test with the string value example:


We recommend the echo command to display the value of this variable:

# echo $test

This command prints the value example in your terminal output.

Note that this variable is only available in the current shell session. To check if this is available in child processes, execute the following bash command:

# bash

Next, verify the variable’s existence with the echo command:

# echo $test

The output produced would be empty, indicating that the test variable exists solely within the parent shell. To make this variable accessible to child processes, it must be exported using the export command.


Exporting variables is crucial in Bash scripting to ensure they are accessible in scripts. 

To better understand, let us consider a script called test-script.sh that attempts to display the value of the test variable.

Open a text editor like Nano or Vim to create the script file. We will use the following command to launch Nano: 

# nano test-script.sh

Next, enter the following lines in the file:


echo $test

Save the changes and close the editor.

Next, ensure the script has execute permissions by running the following chmod command: 

# chmod u+x test-script.sh

You can now run the script with the following syntax:


The output will be empty as the test variable was not exported and is not recognized within the script’s environment.


Now that you know how to set a variable, it is important to understand how to export a variable to ensure the variables are accessible in child processes and scripts.

How to Export a Variable in Bash

The syntax for using the export command in Bash is straightforward. 

To turn a shell variable into an environment variable, return to the parent shell and export it with the Linux export command. The syntax of the command will be as follows:

# export [variable-name]

We suggest using the following printenv syntax to confirm the successful export.

# printenv [variable-name]

Now, when you open a child shell session, echo will return the value of the environment variable:

# echo [variable-name]

How to Export Bash Functions

Exporting a Bash function creates a more robust, maintainable, and reusable scripting environment. This is essential for efficient and error-free development and system administration.

To export Bash functions using the export command, start by defining a Bash function named echoVar:

function echoVar 

This function should execute an echo command to display the value of the test variable:


echo "The value of the test variable is: $test"


The value of the test variable

Once the function is defined, you can invoke it by its name to display the value of the variable. To ensure the function is available in child processes, use the export -f command:

# export -f echoVar

This command exports the echoVar function, making it accessible in child shells.


List All Exported Variables

When the export command is executed without any arguments, it outputs a list of all currently exported variables. 

To ensure these variables are exported to child processes, you can utilize the -p option:

# export -p

This command lists all variables that have been exported, confirming their availability to any child processes.

export -p

To reverse the effects of export -p, utilize the -n option with the export command:

# export -n

This action again restricts the previously exported variables to the current shell session.


Effectively managing and exporting variable bash commands is essential for optimizing the functionality and efficiency of Linux systems. Users can ensure that applications and scripts perform consistently across different sessions and sub-processes through the strategic use of environment variables. 

By mastering these techniques, Linux administrators and developers can enhance system configurability and maintainability, ultimately leading to more robust and adaptable computing environments. 

Whether you’re new to Linux or an experienced professional, the skills to manipulate and export variables are invaluable tools in your technology toolkit.


Q. What is an environment variable in Linux?

An environment variable is a dynamic-named value that can affect how running processes behave on a system. They are used to store information about the operating environment, such as the location of installed files, the current user name, and other system settings.

Q. How do I set an environment variable using Bash?

To set an environment variable in Bash, you can use the syntax VARIABLE_NAME=value. For example, to set the PATH variable, you would type PATH=/usr/local/bin:/usr/bin:/bin.

Q. What does the export command do in Bash?

The export command in Bash marks an environment variable to be exported with any newly forked child processes, allowing those processes to inherit all marked variables. This is crucial for ensuring that sub-processes can access the necessary configurations.

Q. Can you give an example of how to export a variable in Bash?

To export a variable named MY_VAR with the value Hello, World, you would type:

MY_VAR=”Hello, World”

export MY_VAR

This makes MY_VAR available to any child processes spawned from the session.

Q. How can I see all the exported variables in my current Bash session?

You can see all the exported variables by typing export without any arguments in the terminal. This will list all the environment variables currently set and exported in your session.

Q. How do I make the changes to environment variables permanent?

Add them to your profile script to make environment variable changes permanent (~/.bash_profile, ~/.profile, or ~/.bashrc). For example: echo ‘export MY_VAR=”Value”‘ >> ~/.bash_profile

This command appends the export command to the .bash_profile, making the variable available in all future terminal sessions.

Q. What is the difference between setting a variable and exporting it?

Setting a variable makes it available only within the scope of the current shell or script. On the other hand, exporting a variable makes it available to any child processes spawned from the shell or script, thus extending its scope beyond the local environment.

Try this guide with our instant dedicated server for as low as 40 Euros