Shell method to determine if a string is empty

angryTom
Release: 2020-02-07 11:50:55
Original
20173 people have browsed it

This article mainly introduces you to the correct method of determining whether a string is empty in the Linux shell. The article introduces it in great detail through sample code. It has certain reference learning value for everyone to learn or use the Linux shell. Friends who need it Let’s learn together

Shell method to determine if a string is empty

How to determine whether a string is empty in shell

Linux determines whether a string is empty. Two parameters can be used:

● -z: Determine whether the string is an empty string

● -n: Determine whether the 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
Copy after login

Note: When comparing strings, it is a very good habit to use quotation marks to delimit the strings!

Other methods:

if [ "$str" =  "" ]
Copy after login

(Online video tutorial sharing: linux video tutorial)

The above is the detailed content of Shell method to determine if a string is empty. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!