In this article, we explain what Linux environment variables are, define a set of common environment variables and show how to read and set environment variables.
What is Linux?
Linux is a family of open-source operating systems based on the Linux kernel. The first Linux system kernel was released on September 17, 1991, by Linus Torvalds.
Read more …
Popular Linux distributions include Debian, Fedora, and Ubuntu, and the commercial distributions include Red Hat Enterprise Linux and SUSE Linux Enterprise Server.
There are also quite a number of customized Linux distributions, such as Kali Linux, REMnux etc. Kali Linux is a Debian-based distribution developed, funded and maintained by Offensive Security for ethical hackers for the purposes of Penetration Testing, Security Research & Assessment, and Computer Computer Forensics & Reverse Engineering. REMnux, on the other hand, is a Linux distro curated for reverse-engineering and malware analysis purposes.
UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.Dennis Ritchie
Read more educational and inspirational cyber quotes at our page 100+ Best Cyber Security & Hacker Quotes.
What Are Environment Variables?
In Linux, an environment variable is a variable (a.k.a. key) with a name and an associated value (a.k.a. attribute). A variable together with its value is represented in the format
VAR=value1:value2:value3... in the case of multiple values are associated with the variable.
Environment variables are used by applications launched in shells and they can change the way applications behave. Though environment variables are defined by default in Linux, they can also be created, edited, saved or deleted by users to tailor system behavior according to their needs.
Note that environment variables apply to all system processes and shells while the shell variables only apply to the current shell instance.
Below are some of the most common environment variables in alphabetical order:
- EDITOR: Path to the default file editor.
- HOME: The home directory of the current user.
- PATH: A list of directories separated by colons (:), to be searched for to find the applications to execute commands.
- SHELL: Path to the shell for the current user.
- TERM: Default terminal emulator
- USER: The name of the current user.
Listing and Setting Environment Variables
There are a number of ways to list, set and delete environment variables. The available commands that can be showed for these purposes are explained below with examples.
echo – Display a line of text
Echo the STRING to the standard output . Basic usage:
echo [STRING] ... Echo command can also be used to display environment variables by prefixing them with the
$ echo $PATH
env – Run a program in a modified environment
Set each VALUE to VARIABLE in the environment and run COMMAND. Basic usage:
env [OPTION] ... [VARIABLE=VALUE] ... [COMMAND] If no COMMAND is specified, used to list all environment variables. Use
-i to ignore environment variables, i.e., start with an empty environment.
$ env -i /bin/sh
Below example demonstrates running
/bin/sh with an empty environment. As can be observed from the output, the environment variable
MYDUMMYVAR created in the previous shell environment does not return a result in the new shell run with
env -i command.
export – Export attribute to the variables
Set an ATTRIBUTE to the defined environment variable (ENV-VAR) . Basic usage:
export [ENV-VAR[=ATTRIBUTE]] ... For instance, to set vim to the
EDITOR environment variable:
$ export EDITOR=/usr/bin/vim
Note that the
export command changes an environment variable on all environments and the change made is permanent (persists over multiple shell sessions). To change an environment variable only for the current shell session, assign a value to a variable directly, without the
For instance, to run directly an executable that is not on a directory listed on the
PATH variable, change
PATH environment variable temporarily by:
printenv – Print all or part of environment variables
Print the values of the specified environment VARIABLE(s). The default is to print all, if no environment VARIABLE is provided. Basic usage:
printenv [OPTION] ... [VARIABLE] ... Use
-u to unset a variable.
$ printenv PATH
set – Change a run-time parameter
Sets or unsets VARIABLE(s) within the shell environment. When used without an argument it will print a list of all variables (including environment and shell VARIABLEs) Basic usage:
set [OPTION] ... [VARIABLE] ...
unset – Delete shell and environment variables
Delete a shell or environment VARIABLE. Basic usage:
$ unset PATH
Below example demonstrates running
unset command to delete
MYDUMMYVAR environment variable.
To learn more on Linux, you could also read our article Basic Linux Commands for Beginners or visit Linux Resources Page.