Managing Environment Variables in Linux


Introduction

Environment variables are crucial for functioning of an operating system. They are used to store information about system's environment, such as system paths, user preferences, and application settings. In Linux, managing environment variables is an essential task that can be easily done using command-line tools.

In this article, we will explore different methods of managing environment variables in Linux. We will discuss how to view, set, and delete environment variables, and how to make them persistent across sessions.

Viewing Environment Variables

The first step in managing environment variables is to view current list of variables. In Linux, you can use 'env' or 'printenv' command to display a list of all environment variables that are currently set.

Open a terminal and type following command −

$ env 

This command will display a list of all environment variables set in current shell session.

You can also use 'echo' command to view value of a specific environment variable. For example, to view value of 'PATH' environment variable, type −

$ echo $PATH

This command will display value of PATH variable, which is a list of directories where system looks for executable files.

Setting Environment Variables

The next step is to set environment variables. You can use 'export' command to set an environment variable. For example, to set 'EDITOR' environment variable to 'nano', type −

$ export EDITOR=nano

This command will set value of 'EDITOR' variable to 'nano'. You can verify this by typing:

$ echo $EDITOR

This will display value of 'EDITOR' variable, which should be 'nano'.

You can also set multiple environment variables in a single command. For example, to set 'JAVA_HOME' and 'M2_HOME' variables, type −

$ export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
$ export M2_HOME=/usr/share/maven

Persistent Environment Variables

The environment variables that are set using 'export' command are only valid for current shell session. If you close terminal, these variables will be lost. To make environment variables persistent across sessions, you need to add them to a startup file.

In Linux, there are two main startup files: ~/.bashrc and ~/.bash_profile. .bashrc file is executed for each new shell session, while .bash_profile file is executed only for login shells. You can choose which file to use, depending on your needs.

To add an environment variable to .bashrc file, open file using a text editor and add 'export' command at end of file. For example, to make 'JAVA_HOME' and 'M2_HOME' variables persistent, add following lines to .bashrc file −

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export M2_HOME=/usr/share/maven

Save file and close text editor. next time you open a new shell session, environment variables will be set automatically.

Deleting Environment Variables

To delete an environment variable, you can use 'unset' command. For example, to delete 'EDITOR' variable, type −

$ unset EDITOR

This command will delete 'EDITOR' variable. You can verify this by typing −

$ echo $EDITOR

This will display an empty value.

Conclusion

Managing environment variables is an essential task in Linux. In this article, we have discussed different methods of managing environment variables, including viewing, setting, and deleting variables, as well as making them persistent across sessions. We have also explored use of startup files, such as .bashrc and .bash_profile files, to make environment variables persistent.

By understanding how to manage environment variables, Linux users can customize their system's environment to their needs, making it more efficient and easier to use. Environment variables play a vital role in functioning of Linux operating system, and managing them effectively is an important skill for any Linux user.

Overall, managing environment variables in Linux is a straightforward process that can be done using simple command-line tools. With ability to view, set, delete, and make environment variables persistent, users can customize their system's environment to their specific needs, improving their productivity and overall experience.

Updated on: 23-Mar-2023

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements