Mastering Environment Variables on Linux: A Comprehensive Guide

Mastering Environment Variables on Linux: A Comprehensive Guide

Discover the power of Linux Environment Variables and unveil their secrets effortlessly Explore various methods to showcase values, list variables using printenv, and gain insights into commonly used environment variables

Key Takeaways

To view environment variables in Linux, employ the printenv command. This will display all environment variables, nonetheless, for a more navigable output, consider piping it into the less command. Alternatively, utilize grep to examine individual environment variables.

What are Linux Environment Variables?

: Understanding Environment Variables on Linux

Environment variables play a critical role in Linux systems as they store essential values and configurations. Scripts, applications, and shells heavily rely on these variables to customize their behavior and functionality. This guide will cover various methods to display environment variables in your terminal and provide insights on interpreting the resulting list.

Variables store values for us. When a task requires the value, it searches for the corresponding variable in a list and retrieves the value from it. While variable names are immutable, their values can be changed. An environment variable, similar to any other variable, consists of a unique name and a value. The name is assigned during the variable's creation and remains unchanged throughout the lifespan of the environment variable.

On average, our multiple test computers possess 50 environment variables individually.

Changing system environment variables is not a common task, but it can be done if necessary. One instance where you might want to make a change is if you want to increase the size of your Bash shell command history cache. To do this, you can modify the value of the $HISTSIZE environment variable in your ".bashrc" file, which will adjust the maximum number of commands that are remembered.

Although this is a useful and convenient feature, it is not something that you will frequently need to do. Typically, environment variables are either kept at their default settings or modified once and then forgotten. They are not typically adjusted on a regular basis.

Nonetheless, it is beneficial to understand the process of inspecting the environment variables defined and being utilized on your computer. By displaying the environment variables in a terminal window, you gain visibility into their values and gain insight into the elements of your Linux usage that are influenced by these underlying values.

How to Show an Environment Variable's Value

You can use echo to see the value stored in an environment variable. To do that you'll need to know the name of the environment variable in advance.

echo $HOME

echo $USER

Mastering Environment Variables on Linux: A Comprehensive Guide

The env and printenv commands are commonly used on Linux to display the names and values of environment variables. While the printenv command is specifically designed for this purpose, the env command serves a different objective.

env is a tool that enables the execution of an application with temporary environment variables chosen by the user. These temporary variables override the actual values stored and enable the application to operate in a modified environment. If env is called without any command line arguments, it will simply display the list of environment variables.

To ensure accurate checking of environment variables in our examples, we will utilize printenv, a tool specifically designed for this purpose, instead of relying on any potential side-effects caused by incorrect tool invocation.

How to List Environment Variables Using printenv

The usage of the printenv command is simple and efficient to display environment variables. It offers only a limited number of options. By utilizing the --version option, you can determine the release number of the version installed on your computer. Additionally, you can employ the --help command to obtain a brief description of these two options along with another available command line option.

The -0 option is an alternative choice. Typically, printenv displays the environment variables in separate lines, adding a newline character at the end of each line. By utilizing the -0 option, the newline character is substituted with a null byte. This option is useful when you intend to output the results to another application that does not require newline characters.

Mastering Environment Variables on Linux: A Comprehensive Guide

The effect of the -0 option in a terminal window is to cram the output together into an impenetrable wall of text.

Mastering Environment Variables on Linux: A Comprehensive Guide

Interpreting the environment variables mentioned in this format is extremely challenging. It is highly unlikely that you will ever require the -0 option. Therefore, let's eliminate it and give it another go.

printenv

Mastering Environment Variables on Linux: A Comprehensive Guide

The content below:

The output is printed with one environment variable per line. By convention, environment variable names always use uppercase characters. Immediately after the variable name is an equals sign "=", followed by the value that the environment variable is set to.

Mastering Environment Variables on Linux: A Comprehensive Guide

should be rewritten as:

Each line of the output displays an environment variable. According to convention, the names of environment variables are always in uppercase. Immediately after the variable name, there is an equals sign "=", indicating the value assigned to the variable.

Mastering Environment Variables on Linux: A Comprehensive Guide

There's still a lot of output, so you might find it easier to pipe the environment variables into less .

printenv | less

Mastering Environment Variables on Linux: A Comprehensive Guide

This lets you scroll through the list of environment variables, and to search the list as well.

Mastering Environment Variables on Linux: A Comprehensive Guide

To locate the environmental variable you're seeking, use the grep command. For instance, if you are aware that the variable contains the term "display," you can conduct a search within the list in this manner:

printenv | grep DISPLAY

Mastering Environment Variables on Linux: A Comprehensive Guide

Some Common Environment Variables

The default environment variables on different Linux computers may differ depending on the preferences of the maintainers of the various distributions, desktop environments, and shells. As a result, the output of checking environment variables on your Linux device may vary.

These are some of the common environment variables typically found on a Linux computer with the GNOME desktop environment.

BASHOPTS: Contains the list of command line options utilized during the launch of bash.

BASH_VERSION: The version of bash.

COLUMNS: The width of the terminal in columns.

DIRSTACK: The stack of directories for use with the pushd and popd commands.

HISTFILESIZE: The maximum number of lines of command history that can be written to the history file.

HISTSIZE defines the maximum limit for storing command history in memory. Exceeding this limit results in overwriting previously stored commands. Upon closing the terminal window, the command history is saved to the history file.

HOME represents the present home directory of the user.

HOSTNAME: The name of the computer.

IFS: The internal field separator that is used to parse user input. The default value is a space.

LANG: The current language and localization settings, including character encoding.

LS_COLORS: This defines the codes that are used to add color to the output from ls.

MAIL: The path to the current user's Linux mailbox.

OLDPWD: The previous working directory.

The primary command prompt definition determines the appearance of the prompt in your terminal window.

PATH is a list of directories that are searched, in a specific order, for a command or application that matches the one you enter into the shell.

PWD: The current working directory.

SHELL: The name of your default shell.

TERM: The type of terminal that is emulated when you run a shell.

UID: The user identifier of the current user.

USER: The current user.

_: The most recently executed command. If you use printenv to list this, it'll always be printenv.

Environmental Inspections

To retrieve a comprehensive list of environment variables, you can utilize the printenv command. For refining the output, you can employ grep to filter the results based on your preferences. Additionally, you can leverage echo to display the value of a particular environment variable that you are aware of.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld