How to check if a variable is defined in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn fundamental techniques for working with variables in the Linux command line. You will begin by mastering how to display the values of variables using the echo command, a crucial skill for inspecting environment settings and other stored information.

Following this, you will explore how to check for the existence or emptiness of variables using the test command with the -z option, which is essential for conditional logic in scripting. Finally, you will discover how to list all currently defined environment variables using the env command, providing a comprehensive view of your shell's 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/BasicSystemCommandsGroup -.-> linux/test("Condition Testing") linux/UserandGroupManagementGroup -.-> linux/env("Environment Managing") linux/UserandGroupManagementGroup -.-> linux/unset("Variable Unsetting") subgraph Lab Skills linux/echo -.-> lab-558776{{"How to check if a variable is defined in Linux"}} linux/test -.-> lab-558776{{"How to check if a variable is defined in Linux"}} linux/env -.-> lab-558776{{"How to check if a variable is defined in Linux"}} linux/unset -.-> lab-558776{{"How to check if a variable is defined in Linux"}} end

Display variable value 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 type of variable is an environment variable, which stores information about the user's environment, such as the current user, home directory, and terminal type.

To display the value of a variable, you use the echo command followed by a dollar sign ($) and the variable name. The dollar sign tells the shell that you want to see the value of the variable, not the variable name itself.

Let's display the value of the USER environment variable, which stores the name of the currently logged-in user.

Type the following command in your terminal and press Enter:

echo $USER

You should see the output:

labex

This shows that the value of the USER variable is labex, which is your current username in the LabEx environment.

Now, let's display the value of the HOME environment variable, which stores the path to your home directory.

Type the following command and press Enter:

echo $HOME

You should see the output:

/home/labex

This confirms that your home directory is /home/labex.

You can also display the value of the SHELL environment variable, which shows the path to the shell program you are currently using.

Type the following command and press Enter:

echo $SHELL

You should see the output:

/usr/bin/zsh

This indicates that you are currently using the zsh shell.

Using echo with the dollar sign is a fundamental way to access and display the values of variables in the Linux terminal.

Test variable existence with test -z

In this step, you will learn how to check if a variable is empty or not set using the test command with the -z option. This is useful in scripting to make decisions based on whether a variable has a value.

The test command is used to evaluate conditional expressions. The -z option checks if the length of a string is zero. If the string is empty (or the variable is not set), the test is true.

Let's test if a variable named MY_VARIABLE is set. First, we'll make sure it's not set.

Type the following command and press Enter:

unset MY_VARIABLE

The unset command removes a variable. Now, let's use test -z to check if MY_VARIABLE is empty. We'll combine it with echo to see the result of the test. The && operator executes the second command only if the first command succeeds (returns a true value, which for test means the condition is met).

Type the following command and press Enter:

test -z "$MY_VARIABLE" && echo "MY_VARIABLE is empty or not set"

Since we just unset MY_VARIABLE, the test -z "$MY_VARIABLE" condition is true (the variable is empty), so the echo command will execute.

You should see the output:

MY_VARIABLE is empty or not set

Now, let's set a value for MY_VARIABLE.

Type the following command and press Enter:

MY_VARIABLE="Hello"

Now, let's run the same test -z command again.

Type the following command and press Enter:

test -z "$MY_VARIABLE" && echo "MY_VARIABLE is empty or not set"

This time, MY_VARIABLE has the value "Hello", so it is not empty. The test -z "$MY_VARIABLE" condition is false, and the echo command will not execute.

You should see no output from the echo command this time.

This demonstrates how test -z can be used to check if a variable is empty or not set. This is a fundamental concept used in shell scripting for conditional logic.

List all environment variables with env

In this step, you will learn how to list all the environment variables that are currently set in your shell session using the env command.

Environment variables are crucial for how your shell and programs behave. They store configuration information, paths to executable files, and other settings.

The env command, when run without any arguments, prints a list of all environment variables and their values.

Type the following command in your terminal and press Enter:

env

You will see a long list of output, similar to this (the exact output will vary depending on your system and configuration):

SHELL=/usr/bin/zsh
COLORTERM=truecolor
...
USER=labex
...
HOME=/home/labex
...
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
...
PWD=/home/labex/project
...

Each line in the output represents an environment variable in the format VARIABLE_NAME=value.

You can see some of the variables we explored in the previous steps, like USER, HOME, and SHELL. You'll also see many others, such as PATH, which tells the shell where to look for executable commands, and PWD, which shows your current working directory.

The env command is a simple yet powerful tool for inspecting your shell's environment. It's often used for debugging or understanding how a program might behave based on its environment.

Since the output can be quite long, you can pipe the output of env to a pager like less to view it one screen at a time.

Type the following command and press Enter:

env | less

Use the arrow keys to scroll up and down, and press q to exit less.

This step introduced you to listing all environment variables. In the next steps, you'll learn more about how these variables are used and how you can interact with them.

Summary

In this lab, you learned fundamental techniques for working with variables in Linux. You practiced using the echo command with the dollar sign ($) to display the values of environment variables such as USER, HOME, and SHELL, understanding how this command retrieves the stored information.

Furthermore, you explored how to check for the existence or emptiness of a variable using the test command with the -z option, a crucial skill for conditional logic in scripting. Finally, you learned how to list all currently defined environment variables using the env command, providing a comprehensive view of the shell's environment.