How to use shell to determine if a string is empty
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.