Signup/Sign In

Understanding & Modifying Linux environment variables

Environment variables are simply a collection of dynamic named values (a variable name and a corresponding value) that are saved on the system to create an environment which can be utilised by programs that run on Linux and Unix-based systems.

You can alter the system's operation and the actions of its programs using environment variables. For instance, the environment variable may keep track of the API keys, default terminal and colors & shells used by it, the location of executable files, where to store temporary or user files, or the keyboard layout and system locale.

Variable have the following format:

KEY=value
KEY="Some other value"
KEY=value1:value2
  • The names of the variables are case-sensitive. By convention, You should use UPPER CASE names for environment variables.
  • When assigning multiple values to the variable they must be separated by the colon : character.

Important Environment variables

Here is a list of Environment variable which you should keep in mind.

PATH The PATH environment variable represents the directories list that to be searched while implementing the command.
/opt/gradle/bin:/opt/maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin
USER Currently logged in user.
TERM Used for terminal handling.
EDITOR This is the default file editor. We use this editor when we type edit in the terminal.
LOGNAME Represents the current user.
SHELL Shell of the current user.
HISTSIZE This controls the number of lines stored in .bash_history. (HISTFILESIZE defines its file size.)
HOME Home directory of the current user.
PWD Stores the working directory for the specific terminal on which this command is executed.
_ This program.
- Previous working directory.

Print environment variable in Linux terminal

Environment Variables Commands

  1. env: Displays all the environment variables in the shell. It can also be used to run a program in a modified environment.
    Syntax: env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
  2. printenv: Prints all the environment variables and definitions of the ongoing environment.
    Syntax: printenv [OPTION]... [VARIABLE]...
  3. set: Set defines and allocates a shell variable and positional parameters.
    Syntax: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
  4. export: Export a newly allocated environment variable values. Set export attribute for shell variables.
    Syntax: export [-fn] [name[=value] ...] or export -p
  5. unset: Unset values and attributes of shell variables and functions.
    Syntax: unset [-f] [-v] [-n] [name ...]

For an in-depth understanding of using these commands check out the next tutorial.

Persistent Environment Variables

To make the environment variables persist after a reboot/relogin, we must define those environment variables in the configuration files of the shell. In the Linux distributions, the system reads the environment variables from the below files:

  • /etc/environment - File containing system-wide environment variable settings.
  • /etc/profile - We load the variables of this file whenever a user enters a bash login shell.
  • .bashrc - This file is executed only by the user if it's in the home directory of the user.
  • source command can execute any specified file as argument and also load the environment variables inside the current shell session.

Persisting environment variables and print $USER in ~/.bashrc file

Conclusion

In this tutorial, we learnt what are environment variables, their syntax and different types, how we can create/modify them in different contexts and persist them in every session. And, Most important environmental variable that you should always take care of. Please, visit other articles to deep dive into the commands and uses.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.