How to declare boolean variable in shell script

Channel: Linux
Abstract: then echo "It's true"else echo "It's false"fi Sothen echo "It's true" else echo "It's false" fi1234567891011#

The shell (bash) script doesn’t offer any data type with the variables. So we can’t specifically declare a variable of type boolean in shell scripts.

But, you can still use the variable like a boolean in shell scripts. Store a sample string 「true」 to a variable and match it with the if condition. This works similarly to boolean.

#!/usr/bin/env bash # Assign a string to a variable var=true # Test the variable value if [ "$var" = 'true' ]; then echo "It's true" else echo "It's false" fi1234567891011#!/usr/bin/env bash # Assign a string to a variablevar=true # Test the variable valueif [ "$var" = 'true' ]; then   echo "It's true"else   echo "It's false"fi

So, even if the shell script doesn’t offer the data types, but we can still use the normal variable with similar working.

Ref From: tecadmin

Related articles