In Linux environment variables define the system behavior. Environment variables are present in all operating systems like Linux, Mac, Windows, etc. Environment variables are the same as variables. We can declare, recall and set them with the same syntax that we use for variables.

We refer to environment variables by daemons and applications whenever we require them. We use environment variables to revoke the default settings, and with the help of environment variables, we can handle system settings. In this Linux environment variables blog, we will discuss how to set environment variables and how to use them.

Linux Environment Variables - Table of Content

In this blog, we discuss the following topics:

If you want to enrich your career and become a professional in Linux, then visit Mindmajix - a global online training platform: "Linux Certification Training" This course will help you to achieve excellence in this domain.

Linux Environment Variables - A Complete Guide

What is a Variable?

A variable acts as a location to store the value, and that value can be text, filename, and number. Generally, we refer to its representative name that we provide while creating it. The value we store in that variable can be displayed, edited, deleted.

What are Environment Variables in Linux?

Linux Environment variables are the dynamic values that impact the programs or processes on the computer. Environment variables are present in all the operating systems, but data types may differ. Environment variables provide information about system behavior.

[ Related Article: File Permissions in Linux

Importance of Environment Variables

System Administrators have the capability for changing the environment variables to meet individual or greater group requirements of the users inside their environments. Administrators can change the command-line prompt, hostname.

Environment Variables Commands

  1. Env: This command displays all the environment variables in the shell. 
  2. Printenv: This command prints all the environment variables and definitions of the ongoing environment.
  3. Set: This Linux set environment variable defines and allocates an environment variable.
  4. Export: This command exports the newly allocated environment variable values.
  5. Unset: This command deletes the environment variable.

Syntax:

Unset variable name

Example:

$unset $VAR1 

[ Related Article: Advanced Linux Interview Questions

Setting the Environment Variables

To demonstrate the difference between environment and shell variables, we will start setting the shell variables.

To create a shell variable with the name “VAR1” and value “environment,” we have to enter:

$VAR1=’environment’

We can check variable is set through  “echo $MY_VAR”  of refining the set command output with grep set | grep VAR1:

Subscribe to our youtube channel to get new updates..!

$echo $VAR1

We use “printenv” variable for checking whether this variable is an environment variable or not.

$printenv VAR1

We can also attempt to print variable in sub-shell, and we will retrieve an empty output:

$bash -c ‘echo $VAR1’

If you attempt to print the variable in the sub-shell then we will get the variable name displayed on our terminal:

$bash -c ‘echo VAR1’

We can also set the environment variables in one line:

$export “NEW_VAR1” =” NEW VAR1”

[ Related Article: What is Linux? ]

Linux Environment Variables List

Following are some of the important environment variables 

1. USER: Currently logged in user.

Example:

$echo $USER

user

2. EDITOR: This is the default file editor. We use this editor when we type “edit” in the terminal.

3. HOME: This is the home directory of the current user.

Example:

$echo $HOME

/c/user/home

4. LOGNAME: This variable represents the current user.

5. SHELL: This variable represents the shell of the current user.

6. TERM: This variable represents current locales settings.

Example:

$echo $TERM

pterm

7. LANG: This variable represents the current locale settings.

8. PATH: The PATH environment variable represents the directories list that to be searched while implementing the command. When we are executing a command, the system will explore the directories.

Example:

$ echo $PATH

/user/library/document/drive: /user/local/temp

9. MAIL: The MAIL environment variable represents the location where the current user stores his mail.

[ Related Article: Difference Between Linux vs Unix in 2021 ]

Persistent Environment Variables

To make the environment variables constant, we must define those environment variables in the configuration files of bash. In the Linux distributions, when we start a machine, we read the environment variables from the below files:

  • /etc/environment: We use this file for setting up all the environment variables. 
  • /etc/profile- We load the variables of this file whenever we enter a bash login shell.
  • By using the “~/.bashrc:” command, we can declare the variables in the Bash.
  • By using the “Source” command, we can load the environment variables inside the current shell session.

Conclusion

Linux Environment Variables are a group of dynamic values and applications that will use them to customize the system. We can declare and use them as general variables. Among various environment variables, we can use them based on our requirements. I hope this article provides you with the required information about environment variables. 

If you have any queries, let us know by commenting in the below section.