How to List and Set Bash Environment Variables? [3 Methods]

Environment variables are dynamic named values that can affect the way running processes behave. To list environment variables in Bash, use the printenv command. By setting environment variables, it is possible to change the behavior of running processes and make it easier to manage the system. To set an environment variable, use the export command.

From this article, you will learn how to list and set environment variables in Bash.

What is the Environment Variable in Bash?

In Bash, an environment variable is a variable that is part of the environment in which a process runs. These variables are vital and hold information about the environment, and they can be accessed by programs or scripts running in that environment.

Here is a list of a few examples of the environment variables in Bash:

  1. LANG: Specifies the language and localization settings.
  2. HOME: The home directory of the current user.
  3. USER: The username of the current user.
  4. SHELL: The path to the user’s default shell.
  5. PATH: List of directories in which the shell looks for executable files.
  6. PWD: The present working directory.
  7. TERM: Specifies the type of terminal that the user is using.

List Environment Variables in Bash

To display the list of Bash environment variables you can use the printenv command. When you run this command without any arguments, it will output a list of all environment variables and their values.

Go through the following script to list all your system environment variables:

#!/bin/bash

#printing the list of all environment variables
printenv

The printenv command will print the list of all environment variables. Now, let’s run the script:

All environment variables are printed on the terminal.The bash script has printed the list of all the variables on the terminal.

List a Specific Bash Environment Variable

To list a specific Bash environment variable you can search for it from the list of environment variables, using the grep command. Now see the process practically by searching a specified variable, let’s say, the PATH variable:

#!/bin/bash

#looking for the PATH variable in the environment variable list
printenv | grep PATH
EXPLANATION
The output of the printenv command has been piped into the grep command which only looks for the PATH variable.

The Bash script has printed the specific variable 'PATH'.The Bash script has only printed the PATH variable from the list of all environment variables.

3 Methods to Set Environment Variables

You can set bash environment variables using the export command, the set command, and the interpolation method. These approaches set environment variables that are available temporarily. These types of variables are fleeting variables. They are set for the current session and they can not be accessed from different sessions.

Here are 3 easy methods to set environment variables given below:

1. Using the “export” Command

The export command exports environment variables from shell to child processes. At first, you have to define a variable and then export it. The suitable syntax for this purpose is export variable_name.

Here’s how to set an environment variable using the export command in Bash:

#!/bin/bash

#assigning value to VAR variable
VAR="my value"

#exporting VAR
export VAR
echo $VAR
EXPLANATION
The VAR="my value" command has assigned value to the VAR variable. Then the export VAR command has exported the VAR as an environment variable.
The export command has exported the VAR as environment variable.The Bash script has exported the VAR as an environment variable.

2. Using the “set” Command

You can use the set command with the -a option to set environment variables. This option marks variables created or modified for export. By executing the set -a command on the terminal, you can set the variable with value from the terminal. After setting these variables you can access them anytime you want in your current session.

Here’s how to set an environment variable using the set command in bash:

Execute the following command in the terminal:

set -a

It will prepare the terminal to save variables as environment variables. After that, you have to set variables along with the value you wish to set for your environment variables. Here I will set the following two variables with values:

name='John'
age=22

The environment variables named name and age have been set.

Now I can access the previously set name and the age variables from the current sessions. Follow the below script to see how I will access these variables:

#!/bin/bash

echo $name $age

Here echo $name $age command will access the name variable and the age variable from the current session.the set -a command has set two environment variables. From the image see how I accessed the name and the age variable with their values.

3. Using Bash Interpolation

The Interpolation approach is like putting the output of a command to the desired variable and setting it as an environment variable through the export command.

Here’s a Bash script to set environment variables using the interpolation process:

#!/bin/bash

export MYSHELL=$(echo $SHELL)
echo $SHELL
EXPLANATION
In the script, the export MYSHELL=$(echo $SHELL) command has taken the value of the SHELL variable and put it into the MYSHELL variable then the export command has set the MYSHELL variable as the environment variable.

The value of SHELL variable has been saved as an environment variable.The bash script has set the value of the SHELL variable to a new environment variable and then printed it on the terminal.

Set Permanent Environment Variables in Bash

Permanent variables are the variables defined on a system by default and persist across system reboots. These permanent variables prevail on the /etc/environment file. If users need any variable value frequently they can set it as a permanent variable by modifying this file.

To set permanent environment variables you have to place them manually on the /etc/environment file by maintaining the syntax variable_name=variable_value. After placing you can only access them by sourcing the /etc/environment file. Here is the step-by-step process:

    1. First, open the /etc/environment file by executing the below command:
      nano /etc/environment
    2. Now run the following command to assign and export the variable to export this variable:
      GLOBAL="This is a global variable"
    3. To save and exit the text editor press CTRL+O and CTRL+X.
    4. After that, source the “/etc/environment” file by executing the following command on the terminal:
      source /etc/environment
    5. Finally, check the value of the variable GLOBAL from the terminal by running the following command:
      echo $GLOBAL

the process of setting GLOBAL variable as a permanent variable.You can see that the GLOBAL variable has been set as a permanent variable.

NOTE: To learn about how to unset environment variables you can go through this article 2 Ways to Unset Environment Variables Using Bash Script.

Conclusion

Learning the skill of listing and setting bash environment variables is a fundamental skill for developers and system administrators. It helps programmers to continuously monitor and manage the environment variables, enhance software development, and maintain a stable computing environment.

People Also Ask

How to set permanent environment variables in Linux?

To set system-wide Linux environment variables, you have to edit the /etc/environment file and set the variable with value manually to this file. And then source this file to access this variable.

How to check environment variables that exist in bash?

To check whether a variable exists or not in the Bash script, you can use -v or -z ${var} options as an expression with the combination of the “if” conditional command.

How to print the env variable in Linux?

You can print the list of all environment variables on the terminal simply by executing the printenv command.

What is set in the bash script?

The set is a command that allows the bash programmers to enable certain flags in your Bash script to make the script follow certain behaviors and characteristics.

What is $# in the bash script?

The special character $# stores the total number of arguments present. On the other hand, $@ and $* denote all the arguments.

How to create a Bash variable?

To create a variable in bash, you need to assign a value to its reference. You have no need to declare a variable. A variable in bash can contain a number, a character, or a string of characters.

Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment