There are no booleans and there are no boolean expressions in Bash. Every value is a string. The only available data type is a string.
It's typical to represent a boolean value in shell with a string true
and a string false
or a string 0
and a non-zero string as true.
How to assign a variable a boolean expression? Is it even possible without a if statement?
The command [
sets the exit status to 0
if the expression is logically true, and sets to non-zero otherwise. Just set the variable to the value of the exit status of [
command.
wordEmpty=$( [ "$word" = "" ]; echo $? )
if [ "$wordEmpty" -eq 0 ]; then
something
fi
However, because true
and false
are commands that exit with exit status 0
and non-0
respectively, I prefer to use the strings in POSIX shell, because they look nicer.
wordEmpty=$( if [ "$word" = "" ]; then echo true; else echo false; fi )
if "$wordEmpty"; then
something
fi
In Bash shell I prefer numerical values, because I can just do if ((wordEmpty))
.