How to check if a string is empty in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn fundamental techniques for working with strings in Linux. You will discover how to effectively check if a string is empty using the test -z command, a crucial skill for conditional logic in shell scripting. Additionally, you will practice displaying string content using the versatile echo command and explore how to measure the length of a string with the expr length command. These hands-on exercises will provide you with practical experience in manipulating and analyzing strings within the Linux environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicSystemCommandsGroup -.-> linux/test("Condition Testing") linux/TextProcessingGroup -.-> linux/expr("Evaluate Expressions") subgraph Lab Skills linux/echo -.-> lab-558767{{"How to check if a string is empty in Linux"}} linux/test -.-> lab-558767{{"How to check if a string is empty in Linux"}} linux/expr -.-> lab-558767{{"How to check if a string is empty in Linux"}} end

Test string emptiness with test -z

In this step, you will learn how to check if a string is empty in Linux using the test command with the -z option. This is a fundamental operation when working with strings in shell scripting.

The test command is used to evaluate conditional expressions. The -z option specifically checks if the length of a string is zero. If the string is empty, the test is true; otherwise, it is false.

Let's try it out in the terminal. First, we'll define an empty string variable. In shell scripting, you define a variable by typing its name, followed by an equals sign (=), and then the value. There should be no spaces around the equals sign.

Type the following command to define an empty string variable named my_string:

my_string=""

Now, let's use the test -z command to check if my_string is empty. The test command itself doesn't produce output; its result is used to control the flow of a script (which you'll learn later). However, we can see its result by checking the exit status of the previous command using echo $?. An exit status of 0 indicates success (true), and a non-zero status indicates failure (false).

Type the following command to test if my_string is empty:

test -z "$my_string"

After running the test command, check its exit status:

echo $?

Since my_string is empty, the test -z command should evaluate to true, and the echo $? command should output 0.

Now, let's define a non-empty string:

another_string="Hello"

And test if it's empty:

test -z "$another_string"

Check the exit status:

echo $?

This time, since another_string is not empty, the test -z command should evaluate to false, and the echo $? command should output 1.

Understanding how to test for empty strings is crucial for writing robust shell scripts that handle different inputs correctly.

Display string content using echo

In this step, you will learn how to display the content of a string variable using the echo command. As you saw in the first lab, echo is used to print text to the terminal. When used with variables, it prints the value stored in the variable.

Recall from the previous step that we defined a variable named another_string and assigned it the value "Hello".

another_string="Hello"

To display the content of this variable, you use the echo command followed by a dollar sign ($) and the variable name. The dollar sign is important; it tells the shell that you want to use the value of the variable, not the variable name itself as a literal string.

Type the following command to display the content of another_string:

echo "$another_string"

You should see the following output:

Hello

You can also include the variable within a larger string. This is a common way to embed variable values in messages.

Type the following command:

echo "The value of the string is: $another_string"

You should see the following output:

The value of the string is: Hello

Using double quotes (") around the variable name or the entire string is generally recommended. It helps prevent issues with spaces or special characters within the variable's value.

Let's try displaying the empty string we defined earlier, my_string:

echo "$my_string"

Since my_string is empty, this command will simply output a blank line.

The echo command is a fundamental tool for displaying information and debugging in shell scripting. You will use it frequently to see the values of variables and the output of commands.

Measure string length with expr length

In this step, you will learn how to measure the length of a string using the expr command with the length operator. The expr command is used to evaluate expressions, including performing mathematical operations and string manipulations.

The length operator within expr takes a string as an argument and returns the number of characters in that string.

Let's use the another_string variable we defined in the previous steps, which has the value "Hello".

another_string="Hello"

To find the length of another_string, you will use the expr command followed by the length operator and the variable's value. Remember to use the dollar sign ($) before the variable name to get its value.

Type the following command:

expr length "$another_string"

You should see the following output, which is the number of characters in "Hello":

5

Now, let's try finding the length of the empty string my_string:

my_string=""

Type the command:

expr length "$my_string"

The output should be:

0

This confirms that the length of an empty string is indeed zero.

The expr length command is a straightforward way to get the character count of a string in shell scripting. This is useful in various scenarios, such as validating input length or iterating through characters.

Summary

In this lab, you learned how to check if a string is empty in Linux using the test -z command. This command evaluates to true (exit status 0) if the string is empty and false (exit status 1) otherwise. You practiced defining both empty and non-empty string variables and verifying the output of test -z by checking the exit status with echo $?. You also learned how to display the content of a string variable using the echo command and how to measure the length of a string using the expr length command. These fundamental operations are essential for working with strings in shell scripting.