Skip to content

A Guide to Identifying Non-Empty and Non-Space Strings in Bash

On this page

Shell scripts are widely used for automating tasks on UNIX-based systems. One common task in shell scripting is processing text data. Often, we need to identify whether a string is non-empty and non-space, which is a fundamental task in many scripts. In this article, we will discuss how to identify non-empty and non-space strings in shell scripts and provide examples of how to apply this knowledge in your scripts.

1. Identifying Non-Empty Strings

To check whether a string is non-empty, we can use the “-n” option with the “test” command or its equivalent “[[ ]]” syntax. The “-n” option checks if the length of the string is greater than zero. Here is an example:

In this script, we declare a variable “string” and assign it a value of “Hello, World!”. Then, we use the “-n” option with the “test” command to check if the string is non-empty. If the string is non-empty, the “if” statement will be true, and the script will output “The string is non-empty”. Otherwise, it will output “The string is empty”.

2. Identifying Non-Space Strings

To check whether a string is non-space, we can use the “-z” option with the “test” command or its equivalent “[[ ]]” syntax. The “-z” option checks if the length of the string is zero. Here is an example:

In this script, we declare a variable “string” and assign it a value of four spaces. Then, we use the “-z” option with the “test” command to check if the string is empty. However, this check will fail since the string is not empty, but it consists of only spaces. To handle this case, we remove all spaces from the string using the parameter expansion “${string// }”, which replaces all spaces with an empty string. Finally, we use the “-z” option to check if the resulting string is empty. If the resulting string is empty, the “if” statement will be true, and the script will output “The string is all spaces”. Otherwise, it will output “The string is not all spaces”.

3. Combining Non-Empty and Non-Space Checks

To check whether a string is both non-empty and non-space, we can combine the “-n” and “-z” options with the “test” command or its equivalent “[[ ]]” syntax. Here is an example:

In this script, we declare a variable “string” and assign it a value of ” Hello, World! “. Then, we use the “-n” and “-z” options with the “[[ ]]” syntax to check if the string is both non-empty and non-space. If both checks are true, the “if” statement will be true, and the script will output “The string is non-empty and non-space”. Otherwise, it will output “The string is either empty or contains spaces”.

Conclusion

Identifying non-empty and non-space strings is a fundamental task in many shell scripts. By combining the “-n” and “-z” options with the “test” command or its equivalent “[[ ]]” syntax, we can easily check whether a string is both non-empty and non-space.

In conclusion, mastering shell scripts requires a good understanding of string manipulation and testing. By combining the techniques discussed in this article, you can easily identify non-empty and non-space strings in your scripts, and handle them accordingly.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top