Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use shell to determine if a string is empty

Shulou Source: shulou.com Published: 2022-06-03 06:39:27 09月18日 Update

Today, the editor shared to you how to use shell to judge the string is empty, I believe many people do not understand, in order to let you know more about shell how to judge the string is empty, so give you a summary of the following content, let's look down. I'm sure you'll get something.

The method of judging string to be empty by shell

To determine whether the string is empty under Linux, you can use two parameters:

●-z: determine whether string is an empty string

●-n: determine whether string is a non-empty string

Example:

#! / bin/sh STRING= if [- z "$STRING"]; then echo "STRING is empty" fi if [- n "$STRING"]; then echo "STRING is not empty" fi root@desktop:~#. / zerostring.sh STRING is empty

Note: when comparing strings, it is a very good habit to define strings in quotation marks!

Other methods:

If ["$str" = ""]

On how to use shell to judge the string is empty to share here, of course, not only the above and everyone's analysis methods, but the editor can ensure its accuracy is absolutely no problem. I hope that the above content can have a certain reference value for everyone, and can be put into practice. If you like this article, you might as well share it for more people to see.

Tags: Characters strings contents methods empty strings put knowledge into practice two values examples accuracy methods parameters quotation marks articles right and wrong more see problems guarantees analysis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Huawei Redmi Xiaomi