How can I check if a variable is empty in Bash?
PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.
To check if a variable is set in Bash Scripting, use-v var or-z ${var} as an expression with if command. This checking of whether a variable is already set or not, is helpful when you have multiple script files, and the functionality of a script file depends on the variables set in the previously run scripts, etc.
Check if String is Empty using -z String Operator -z string operator checks if given string operand's size is zero. If the string operand is of zero length, then it returns true, or else it returns false. The expression to check if string is empty is [ -z "$s" ] where s is string. String is empty.
In Bash at least the following command tests if $var is empty:
if [[ -z "$var" ]]; then # Do what you want fi
The command man test
is your friend.
Presuming Bash:
var="" if [ -n "$var" ]; then echo "not empty" else echo "empty" fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With