Environment variables are used to modify the behavior of an environment. These variables can change how software or application works. Setting up environment variables has multiple applications in bash. Such variables can be used to store anything.
Moreover, every system has certain environment variables that it uses while interacting with the user. In this article, we will first look at the basics of bash scripts and set up and manipulate environment variables and then go through some remedies for the associated problems.
In Ubuntu, nearly every task can be executed using either the Graphical User Interface or the Command Line Shell. Some tasks are more easily done using the shell rather than the GUI. Scripts are files that consist of commands. All of these commands are executed when the script file is executed. Bash scripts are such scripts that use the Bash interpreter.
The extension of the scripts is .sh. Scripts can be written using any text editor. Each bash script starts with the line #!/bin/bash, which tells the system to use the bash interpreter.
Environment variables have the properties of process locality, inheritance, and case sensitivity. Process locality means that the environment variables are exclusive to the specific instance of the shell unless specified otherwise. Inheritance refers to the parent-child relationship between various environments.
Case sensitivity, as the name implies, means that the environment variables are sensitive to case changes. Generally, the format of environment variables is dependent on its parent system. However, all of the environment variables have two features in common; variable name and stored value.
The variable name can be anything, but the value must be in the format compliant with the environment variable type. This is especially important when dealing with the environment variables created by the system. For example, the environment variable LANG is responsible for selecting the language that the application uses to communicate with the user. Its value is location-dependent, but typically in the US, it will have the value “en_US.UTF-8”.
Manipulating Environment Variables
There exist several graphical utilities for looking, setting up, and editing environment variables, but in this article, we will be looking at how to deal with such variables in the bash command line.
To create a new environment variable, you can use the export command. There are multiple ways to create an environment variable. You can define the variable first and then export it. We will create the variable named EDITOR with the value nano (text editor) and then export it.
$ export EDITOR
The other way of creating the environment variables is by defining them in the export command itself.
If you intend to use spaces in the value, you need to use double quotations while creating the environment variable as shown:
The values of the environment variables can be accessed by using the echo command followed by the name of the respective variable with a dollar sign.
You can also use the printenv command to get the value of any environment variable.
Issues related to environment variables
While defining environment variables in bash scripts, the common issues are related to the parent-child relation of shells. The solution to such issues is to define the variables in a parent environment. As we saw earlier, typically, the environment variables are temporary and exclusive to the shell instance in which they are created; however, we can also define environment variables that are persistent and available to other users.
If you want to create an environment variable that is permanent for your use only, you will have to edit the .bashrc file. It is located in the /home/user directory. You can add a VAR environment variable in the .bashrc file by executing the following commands:
Now, we use the nano text editor to modify the contents of the bash file.
To apply the changes to your current session, source the .bashrc file by using the following command:
The variable VAR will be available for every instance of the shell run by the current user.
However, if you are looking to create an environment variable that is available for all the users, you will have to include it in the /etc/environment file. For example, we can add a GLOBAL environment variable to the /etc/environment file by executing the following commands:
As we did before with the .bashrc file, we now edit the contents of the environment file.
Source the environment file to apply the changes by typing the following command:
If you want to create an environment variable that stores the result of a command executed in a bash script, you will have to use parameter substitution. The general format of parameter substitution involves usage of the export command followed by the command with a dollar sign enclosed in brackets as shown:
For example, if you want to store your SHELL environment variable in another variable MYSHELL, you can use the following command:
In this article, we have gone through the basics of bash scripts and environment variables. Moreover, we have understood how to manipulate the environment variables, some typical issues related to creating such variables in bash scripts, and how to remedy them.