Linux Environment Variables

Filed Under: UNIX/Linux

Introduction

An Environment is an area that contains information about how various programs and applications behave. Linux Environment Variables are used by the applications to get information about the environment.

The Environment is configured every time a new shell session is created. Changing the Linux Environment Variables helps the user to control how they interact with programs via the shell, what default values are used for the appearance of the shell window, the type of shell used, default home directory, etc.

Linux Environment Variables File

Information required for setting up the Environment is obtained via various files through a process known as Initialization. The operating system performs this process by reading through /etc/profile and .profile files.

The /etc/profile file is under Linux administrative control and contains essential information which cannot be edited by the user. The .profile file is fully under user control and can be used to modify the environment.

If you are using the bash shell, the user environment variables can be created in .bash_profile and .bashrc file.

If these files don’t exist, the operating system proceeds with creating the Environment and does not display any error.

The terminal prompts the user for input (indicated by the $ sign) after the Environment has been set up.

Linux Environment Variables

These are name/value pairs.

Environment variables can be handled using the following 5 main commands.

  1. printenv
  2. env
  3. set
  4. export
  5. unset

Let’s look into each of these commands one by one.

  1. printenv can be used to print the value of an environment variable. If no key is specified, then all name-value pairs are printed as output. These only include variables with a global scope.
    printenv command example

    printenv command example

    Linux Environment variables are defined in capital letters by convention.

  2. env also prints the name-value pairs like printenv. This command also only prints variables with global scope. However, it cannot be used to print specific values of environment variables.
  3. set is used to print shell variables, environment variables, local variables and shell functions. These include variables with both local and global scope.

    set command example

    set command example

  4. export is used to create new environment variables that persist even after a shell session has been closed.
  5. unset is used to unbind or remove an environment variable.

Some Important Pre-Defined Environment Variables

  • LANG: Contains current system language.
  • USERNAME: Contains username of the current user.
  • PWD: Contains path of the current directory.
  • PATH: Contains colon separated list of directories in which system looks for executables.
  • HOME: Contains path of the home directory.
  • SHELL: Contains path to the user’s preferred shell.
  • EDITOR: Contains command to run lightweight editor. Eg: /usr/bin/nano
  • VISUAL: Contains command to run editor used for heavy editing tasks. Eg: Vi (Vim)
  • BROWSER: Contains command to run default web-browser.

Environment Variable Operations

Let’s look at some common environment variable operations in Linux.

1. Creating New Environment Variables

New variables can simply be created by typing a new variable name and assigning it a value in "", separated by = sign.

No spaces must be there between = and variable name/value. Otherwise, the terminal will return an error.

Once the variable is created, its values is displayed using the echo command.

new env var example

new env variable example

A $ sign must precede the variable name while displaying the value using echo, as shown in the example above. However, it is not required while creating a new variable.

Now that we have created a new Environment Variable, let’s try finding it in the list of variables displayed by the printenv command, and printing out its value.

This can be done using the grep command.

grep env var value

grep env variable example

No output is displayed. This is because even though we have defined a new Environment Variable, we are yet to “export” it into the file containing names of all variables.

Exporting allows the variable to be accessible by other programs and subshells.

This is achieved by using the export command we discussed in the last section.

$ export [VARIABLE_NAME] 
export command example

export command example

Value of the variable is now displayed. It can even be accessed in a new subshell.

env var subshell export example

subshell export example

We can exit the subshell by typing the exit command. This brings us back to the original shell.

2. Changing Values of Existing Variables

Existing variables can be edited by simply assigning a new value to them. We can see the changes made by printing the variable value using echo.

changing env var value

changing env variable value

3. Removing Environment Variables

The unset command is used to remove existing Environment variables.

$ unset [VARIABLE_NAME] 
unset command example

unset command example

Persistent vs Non-Persistent Environment Variables

All Environment Variables that we have dealt with till now, have been non-persistent variables. The changes made to these only last until the current shell is active and do not affect new shell windows. Once the current session is closed, all changes made to variables – created or edited, are lost.

In order to preserve changes through new sessions and windows, we can create Persistent Environment Variables. These are created by defining variables within the environment variable file in the home directory – .profile, .bash_profile, and .bashrc.

We can open the .bashrc file using nano or vim.

persistent env var example

accessing .bashrc file

Define a new variable anywhere inside the .bashrc file using the export command. Save changes and exit the file.

new env var in bashrc

defining env variable in .bashrc

Close the current terminal session and open a new one.

Now, print the Environment Variable just created using the echo command.

Persistent Env Var Display

persistent env variable output example

We can see that the variable persists even after creating a new terminal session and closing the previous one.

Conclusion

Environment variables are very important and useful. They can modify the working of programs and make it easier for the user to interact with them. Existing variables can be easily modified according to the user’s need. Moreover, new variables can also be created easily. Environment Variables can change how the user interacts with the shell and even make it quicker to access certain programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages