How to check if an environment variable is set in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn fundamental techniques for working with environment variables in Linux. You will explore how to display the value of a specific variable using the echo command, verify if a variable is set and display its value using the printenv command, and list all currently set environment variables using the set command. These skills are essential for understanding and interacting with the Linux shell and system environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/UserandGroupManagementGroup -.-> linux/env("Environment Managing") linux/UserandGroupManagementGroup -.-> linux/set("Shell Setting") subgraph Lab Skills linux/echo -.-> lab-558782{{"How to check if an environment variable is set in Linux"}} linux/env -.-> lab-558782{{"How to check if an environment variable is set in Linux"}} linux/set -.-> lab-558782{{"How to check if an environment variable is set in Linux"}} end

Display variable with echo

In this step, you will learn how to display the value of a variable using the echo command. Variables in Linux are used to store information that can be accessed and used by the shell and other programs.

A common variable you'll encounter is $HOME, which stores the path to your home directory. Let's display its value.

Type the following command in your terminal and press Enter:

echo $HOME

You should see the path to your home directory printed to the terminal, which is typically /home/labex in this environment.

/home/labex

The $ symbol before the variable name is crucial. It tells the shell that you want to see the value of the variable, not the literal string "$HOME".

Now, let's try displaying a variable that doesn't exist.

Type this command and press Enter:

echo $MY_VARIABLE

You won't see any output. This is because the variable MY_VARIABLE has not been set, so its value is empty.

You can also display multiple variables at once:

echo $HOME $USER

This will print the value of the $HOME variable followed by the value of the $USER variable (which is your username, labex).

/home/labex labex

Understanding how to display variables is a fundamental skill in Linux, as variables are used extensively in scripting and system configuration.

Click Continue to proceed to the next step.

Verify variable with printenv

In the previous step, you used echo to display the value of a variable. Another useful command for working with environment variables is printenv.

printenv is specifically designed to print the values of environment variables. Environment variables are a special type of variable that are available to processes running in the system.

Let's use printenv to display the value of the $USER variable.

Type the following command in your terminal and press Enter:

printenv USER

You should see your username, labex, printed to the terminal.

labex

Notice that when using printenv, you do not use the $ symbol before the variable name. printenv expects the variable name itself as an argument.

What happens if you try to print a variable that doesn't exist using printenv?

Type this command and press Enter:

printenv MY_VARIABLE

Similar to echo, you won't see any output because the variable MY_VARIABLE is not set as an environment variable.

While echo can display any variable, printenv is specifically for environment variables. In many cases, you can use either, but printenv is often preferred when you specifically want to work with environment variables.

Click Continue to move on.

List all variables with set command

In the previous steps, you learned how to display individual variables using echo and printenv. But what if you want to see all the variables that are currently set in your shell environment?

The set command, when used without any arguments, will list all shell variables, including environment variables and shell-specific variables.

Type the following command in your terminal and press Enter:

set

You will see a long list of variables and their values. This output includes environment variables (like HOME, USER, PATH) as well as variables specific to your current shell session.

...
HOME=/home/labex
HOSTNAME=...
HOSTTYPE=x86_64
...
LANG=C.UTF-8
...
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
...
SHELL=/usr/bin/zsh
...
USER=labex
...

The output can be quite extensive. You can scroll through it to see the different variables that are set by default in your environment.

Comparing the output of set and printenv (if you were to run printenv without arguments), you would notice that set includes more variables. This is because set shows all shell variables, while printenv only shows environment variables.

Understanding the difference between shell variables and environment variables is important as you delve deeper into Linux. Environment variables are inherited by child processes, while shell variables are typically local to the current shell session.

You have now learned three ways to interact with variables in Linux: echo (for displaying individual variables), printenv (for displaying environment variables), and set (for listing all shell variables).

Click Continue to complete this lab.

Summary

In this lab, you learned fundamental methods for interacting with environment variables in Linux. You first explored using the echo command with the $ prefix to display the value of a specific variable, understanding that an unset variable will produce no output. You also practiced displaying multiple variables simultaneously with echo. Subsequently, you were introduced to the printenv command, a dedicated tool for verifying and displaying the values of environment variables.