Home  >  Article  >  Operation and Maintenance  >  Shell method to determine if a string is empty

Shell method to determine if a string is empty

angryTom
angryTomOriginal
2020-02-07 11:50:5520373browse

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

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

Other methods:

if [ "$str" =  "" ]

(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!

Statement:
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 admin@php.cn