Art, Programming, Linux

Andrew's notes

Home Posts Tags

Environment variables in Linux

A small but comprehensive guide to setting and verifying environment variables in debian-based Linux systems

Published on June 20, 2023 1021 words 5 min read Linux

Image by Clay Banks

In the world of Linux, environment variables play a crucial role in customizing system behavior and providing configuration options for various applications. Understanding how to set and verify environment variables is essential. In this guide, we will dive into the intricacies of environment variables and walk you through the step-by-step process of setting and verifying them in Debian.

First, lets take a look at what exactly is an environment variable. Environment variables are dynamic values that can be set at the operating system level and are accessible to processes and programs running on a system. They provide a way to customize and configure the behavior of applications, define system-wide settings, and provide information about the system’s environment. Environment variables are widely used in Linux and other operating systems to influence the behavior of software, determine search paths for executables, specify language settings, define temporary file locations, and more.

There are several commonly used environment variables that play a significant role in configuring and customizing the behavior of systems and applications. One such variable is “PATH,” which specifies the directories where the operating system looks for executable files. Another important variable is “HOME,” which represents the user’s home directory. “LANG” defines the default language setting for the system, while “TMPDIR” points to the directory for temporary files. “DISPLAY” specifies the X server display to use for graphical applications. Additionally, “JAVA_HOME” indicates the installation directory of the Java Development Kit (JDK). These are just a few examples of the multitude of environment variables available, each serving a specific purpose in system configuration and application functionality.

To check the environment variables and their respective values on your system, you can use the command-line interface. In Linux, open a terminal and type the command printenv or env and press Enter. This will display a list of all environment variables currently set on your system, along with their corresponding values. The output will provide you with a comprehensive overview of the environment variables available, allowing you to examine and access their values as needed. This can be particularly useful for troubleshooting, verifying specific settings, or gaining insights into the configuration of your system.

Setup a temporary environment variable #

To set a temporary environment variable, you can use the command-line interface in your operating system. In Linux, open a terminal and use the “export” command followed by the variable name and its desired value, separated by an equal sign. For example, to set a temporary variable named “MY_VARIABLE” to the value “Hello World” you would need to open a terminal and enter :

export MY_VARIABLE="Hello World"

To check the value of a single environment variable like MY_VARIABLE you can use the echo command. It should return ‘Hello World’.

echo $MY_VARIABLE

This sets the variable only for the current session, and it will not persist beyond that. This approach is useful when you need to define a variable temporarily for a specific task or session without affecting the system-wide environment. Once you close the terminal or start a new session, the temporary variable will no longer be available.

Setup a permanent environment variable #

Permanent environment variables are those that persist across different sessions and remain available even after restarting the system. These variables are typically set in configuration files specific to the user or system. In Linux, a common approach is to define permanent environment variables in the user’s ~/.bashrc or ~/.profile file. By adding the necessary export statements to these files, the variables become available every time a new terminal session is started. Additionally, system-wide environment variables can be set in the /etc/environment file, which ensures their availability to all users on the system. Permanent environment variables are particularly useful for configuring default settings, defining paths for executable files, specifying default language settings, or providing application-specific configurations.

The main difference between setting up an environment variable in ~/.bashrc and ~/.profile lies in their purpose and scope:

  • .bashrc: This file is specific to the Bash shell and is executed for each interactive Bash session. It is typically used for defining shell-specific configurations, aliases, and functions. Environment variables set in ~/.bashrc are specific to the Bash shell and are not automatically inherited by other types of shells or non-interactive sessions.

  • .profile: This file is more general and is executed by various shell types, including Bash. It is executed once during login and is responsible for setting up environment variables and other configurations that are applicable to the entire user session, regardless of the shell type. Environment variables set in ~/.profile are inherited by both interactive and non-interactive shells.

In summary, if you want an environment variable to be available in all types of shells and sessions, you should set it in ~/.profile. On the other hand, if you only want the variable to be available in interactive Bash sessions, you can set it in ~/.bashrc. However, in Debian-based systems, the default configuration usually sources ~/.bashrc from ~/.profile, which means that setting the variable in either file will usually achieve the desired result.

We will use the ~/.profile to setup our environment variable. There is only one additional setup to setting up a permanent environment variable compared to a temporary and that is editing the aforementioned file.

sudo nano ~/.profile
# OR 
sudo nano ~/.bashrc

Once you are editing the file, simply add export MY_VARIABLE="Hello World" at the end, save and exit(CTRL+X and then Y). For the variable to take effect you need to either log out and log back in or type source ~/.profile (or bashrc) in the terminal. To verify that your environment variable works you can echo $MY_VARIABLE like we did in the previous example with the temporary environment variables.

Conclusion #

Thats it ! Now you know how to setup environment variables in a debian-based system. Ofcourse in a realistic scenario your variables might be a tad more complicated than what we did here but the gist is just that.

  1. If you would like to read more about environment variables in Debian you can read the official documentation.
  2. If you are curious about environment variables in general there is a great article at Wikipedia.

---

If you found this post useful please consider donating a small amount to support my effort in creating more content like this.