FUNDAMENTALS A Complete Guide for Beginners
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:
- LANG: Specifies the language and localization settings.
- HOME: The home directory of the current user.
- USER: The username of the current user.
- SHELL: The path to the user’s default shell.
- PATH: List of directories in which the shell looks for executable files.
- PWD: The present working directory.
- 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:
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
printenv
command has been piped into the grep command which only looks for the PATH variable.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
VAR="my value"
command has assigned value to the VAR variable. Then the export VAR
command 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. 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
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 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:
-
- First, open the
/etc/environment
file by executing the below command:nano /etc/environment
- Now run the following command to assign and export the variable to export this variable:
GLOBAL="This is a global variable"
- To save and exit the text editor press CTRL+O and CTRL+X.
- After that, source the “/etc/environment” file by executing the following command on the terminal:
source /etc/environment
- Finally, check the value of the variable GLOBAL from the terminal by running the following command:
echo $GLOBAL
- First, open the
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
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- 2 Cases to Execute Command Stored in Bash Variable
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial