Home > Article > Operation and Maintenance > Shell method to determine if a string is empty
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
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!