Afterdeclaring a string variable, use the-zoperator in anif statementto check whether a string is empty or not: MY_STRING="" if [ -z $MYSTRING ] then echo "String is empty" else echo "String is not empty" fi For more Shell scripting tips,check out or Bash/Shell scripting articles!
-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 ...
Bash技巧:对比 test判断字符串是否为空的用法,#!/bin/bashfunctionempty_string(){iftest-n$1;thenecho'(1)-n$1:'"Noquote:notempty."fiif[-z$1];thenecho'(2)-z$1:'"Noquote
# remove the last character in string and return it in $rval if [ -z "$1" ]; then # empty string rval="" return fi # wc puts some space behind the output this is why we need sed: numofchar=`echo -n "$1" | wc -c | sed 's/ //g' ` if [ "$numofchar" = "1" ]...
}if(_rl_digit_p (string[i])) {/* Get the extent of the digits and compute the value. */for(which =0; _rl_digit_p (string[i]); i++) which = (which *10) + _rl_digit_value (string[i]); *caller_index = i;if(sign <0) ...
is readable-s FILE_NAM # TrueifFILE_NAM existsandisnotempty-w FILE_NAM # TrueifFILE_NAM has write permission-x FILE_NAM # TrueifFILE_NAM is executable#字符串测试操作-z STRING # TrueifSTRING is empty-n STRING # TrueifSTRING isnotemptySTRING1...
-v varname True if the shell variable varname is set (has been assigned a value, even an empty value). -R varname True if the shell variable varname is set and is a name reference. -z string True if the length of string is zero. ...
if [ -z "$1" ]; then # empty string rval="" return fi # wc puts some space behind the output this is why we need sed: numofchar=`echo -n "$1" | wc -c | sed 's/ //g' ` if [ "$numofchar" = "1" ]; then
-z: Empty string -n: Non-empty string 3. File and Directory Checks: -e: Exists -f: Regular file -d: Directory -r: Readable -w: Writable -x: Executable Example of using If Statement: Let’s illustrate the usage of if statements with a few examples: ...
myArray is NOT empty and contain 3 elements How to check if a Bash Array contains a value? There is no in array operator in bash to check if an array contains a value. Instead, to check if a bash array contains a value you will need to test the values in the array by using a...