5 minutes read

Environment variables are an integral part of the operating system. Being able to fully utilize them gives us flexibility and freedom as programmers. In this topic, you will learn to list environment variables, create new environment variables, and use them to your advantage. You will also learn about persistent environment variables and how to create them.

List environment variables

A few handy commands can list available environment variables in your operating system. One of them is the printenv command. Using this command will print all the environment variables associated with the system.

$ printenv

Running the above command in a Linux system gives the following result.

HOME=/home/kali
LANG=en_US.UTF-8
LANGUAGE=
LOGNAME=kali
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/kali/.dotnet/tools
SHELL=/usr/bin/zsh
TERM=xterm-256color
USER=kali
WINDOWID=0
.....

To display the value of each individual variable, you may use printenv variable_name . For example,

$ printenv PWD

This will print the value stored in the PWD variable.

/home/kali

How about creating environment variables?

The export command

Typically, a variable declared in a script or bash terminal exists only within that terminal or scope. The export command allows us to set any variable as an environment variable. We can either directly declare an environment variable;

export NEW_VARIABLE="The export command"

or instead, declare a variable and export it to turn it into an environment variable;

NEW_VARIABLE="The export command"
export NEW_VARIABLE

In both cases, the result is the same. An environment variable is created for that particular process. These variables are also passed to newly created child processes. However, the variable is still temporary and will be lost when the process it is associated with ends.

If you want to remove the environment variable, you can use the unset command.

unset NEW_VARIABLE

Persistent environment variables

The variables you have created and used up to this point are temporary. When a process terminates its environment variables are also lost. This means that restarting the shell terminal or the computer will reset everything and you will lose your environment variables. However, there are ways to create an environment variable that is persistent, i.e. creating an environment, not in the context of a process but instead saving them in specific files used by the operating systems like /etc/environment, /etc/profile , and ~/.bashrc instead of the terminal. Each file caters to a different purpose:-

  • /etc/environment is a system-wide configuration file used to store path and locale settings in the form of name-value pairs.
  • /etc/profile is a startup script file. Commands in this file are executed after user login.
  • ~/.bashrc is a user-specific startup script file that contains configurations specific to the terminal. Commands in this file are executed when you run a new bash terminal.

If you want to create persistent environment variables, you need to include them in the specific files mentioned above. Modifying the values of the existing variables will give different results based on what those variables are used for. Since /etc/profile and ~/.bashrc are initialization scripts, including commands to create environment variables within them will create those variables at every execution of those scripts. You can use the export command within these files to create persistent environment variables.

Some persistent environment variables are:

  • PATH — A list of directories separated by colons. When you execute commands in the terminal without specifying the path, the system checks for a path to a directory that contains the command.
  • TEMP — The location where processes can store temporary files.
  • HOME — The path to where the home directory is located.
  • USER — The currently logged-in user.
  • EDITOR — The default text editor. Linux uses this editor when you type edit in your terminal.
  • SHELL — The path of the current user’s shell, such as bash or zsh.
  • LANG — The current locales settings.

Do not modify system environment variables without their in-depth knowledge.

Conclusion

Environment variables are extremely essential to the functioning of the operating system. They help you move from hard-coded variables inside programs to a more dynamic method of managing such values. This helps manage data and mitigate risks. As a programmer, environment variables are a great addition to your arsenal. But, environment variables are also a part of the operating system. Being able to use them allows us new ways to work with our computers.

50 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo