Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

A set of dynamic variables that can affect the running processes behavior and access to resources.

Introduction

Environment variables in Unix-like operating systems determine the behavior and access in the system. Some of the settings are contained within configuration settings, other are determined by user input. The shell keeps track of all the settings in the environment, that builds every time it starts a session with variables, defining system properties. Environment variables by convention are defined using capital letters.

The list of environmental variables is show by using commands env and printenv :

$ printenv
XDG_VTNR=7
LC_PAPER=en_GB.UTF-8
LC_ADDRESS=en_GB.UTF-8
XDG_SESSION_ID=c2
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/aav
LC_MONETARY=en_GB.UTF-8
CLUTTER_IM_MODULE=xim

printenv can request for particular variable

$ printenv SHELL
/bin/bash

env can set variables like:

$ env test_var="The test"
$ echo $TEST_VAR
The test

Shell variables are shown with the set command:

$ set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()

Creating shell variable:

$ SOME_VAR="test shell var"

This variable is defined only for the shell.

$ set | grep SOME_VAR

But not in the environment variables:

$ printenv | grep SOME_VAR 

Creating environment variable Shell variables can be turned into environment variables

$ export SOME_VAR

$ printenv | grep SOME_VAR

Environment variable can be turned back to shell variables

$ export -n SOME_VAR

Common environment variables

PATH – a list of directory paths. When the user types a command without providing the full path, this list is checked to see whether it contains a path that leads to the command.

HOME (Unix-like) - dicate where a user's home directory is located in the file system.

TERM – specifies the type of computer terminal or terminal emulator being used (e.g., vt100).

MAIL – used to indicate where a user's mail is to be found.

SHELL - default shell

PWD - prints current working directory

Reference and further reading

Is there a “.bashrc” equivalent file read by all shells?

What is the difference between 'env' and 'printenv'?

How to permanently set environmental variables

How To Read and Set Environmental and Shell Variables on a Linux VPS

history | excerpt history