-z is the second supported bash string comparison operator used to check if a string is empty or not. The -z operator functions similarly like -n operator. Below is an example: Most importantly, you should add spaces around the square brackets. If there are no spaces, bash will complain ...
str="" if [ "$str" == "" ]; then echo "String is empty" else echo "String is not empty" fi 字符串长度比较:可以使用大于号(>)、小于号(<)来比较两个字符串的长度。例如: 代码语言:txt 复制 str1="Hello" str2="World" if [ ${#str1} -gt ${#str2} ]; then echo "str1 is lo...
echo "-n $a : The string length is not 0" else echo "-n $a : The string length is 0" fi if [ $a ] then echo "$a : The string is not empty" else echo "$a : The string is empty" fi 结果 abc = efg: a != b -n abc : The string length is not 0 abc : The strin...
echo "The string is not empty." fi 这是我们执行脚本时的结果: 代码语言:txt AI代码解释 $ ./test.sh Strings are different. 例4 -n运算符还可用于测试字符串长度是否不为零。 代码语言:txt AI代码解释 #!/bin/bash string="hello" if [[ -n $string ]]; then echo "The string is not empty...
function empty_string() {iftest -n $1; then echo'(1) -n $1 :' "No quote: not empty."fiif[ -z $1]; then echo'(2) -z $1 :' "No quote: empty."fiiftest -n "$1"; then echo'(3) -n "$1":' "Quote : not empty."fiif[ -z "$1"]; then ...
string="hello" if [[ -n $string ]]; then echo "The string is not empty." else echo "The string is empty." fi 这是我们执行脚本时的结果: $ ./test.sh Strings are different. 例5 我们还可以使用小于<和大于>运算符来检查一个字符串是否比另一个字符串多。
/bin/bashfunction empty_string() { if test -n $1; then echo '(1) -n $1 :' "No quote: not empty." fi if [ -z $1 ]; then echo '(2) -z $1 :' "No quote bash 原创 已注销 2024-03-26 10:38:55 42阅读 mysql字符串字段为空...
-n STRING STRING True if string is not empty. 即,test命令使用-z STRING操作符来判断STRING字符串的长度是否为 0。 如果为 0,就是空字符串,会返回 true。 具体写法是test -z STRING,使用[命令则写为[ -z STRING ]。 -n STRING操作符判断STRING字符串的长度是否为 0。
string="" if [[ -z $string ]]; then echo "The string is empty." else echo "The string is not empty." fi 1. 2. 3. 4. 5. 6. 7. 8. 9. 这是我们执行脚本时的结果: $ ./test.sh Strings are different. 1. 2. 例4
在这个程序中,String 是一个非空变量。由于 -n 运算符返回 true,如果 string 的长度不是 0,因此我们得到 The variable String is not an empty string. 作为给定程序的输出。 检查一个字符串是否包含子字符串 要检查一个字符串是否包含子字符串,我们可以使用 =~(Regex)运算符。