Note that bash
does not natively support a bool
type operator. All the commands/built-ins return an exit code to the shell depending upon its success/failure status. A code 0
to the shell means the operation is success and any non-zero would represent a failure.
So in bash
, you do the comparison and need to set the bool
strings explicitly, something like
[[ $a == $b ]] && echo true || echo false
Note that using echo true
should not confused with the built-ins /bin/true
and /bin/false
which explicitly set the exit code to the shell to 0
and 1
respectively. The echo
statement just prints out the string mentioned to the standard output.
Also note that [[
is a shell keyword extension to the POSIX-ly available [
construct. The former is a added to the bourne again shell and may not available in other POSIX compliant shells. If you are looking for a portable way of doing this, use the [
with the case
construct as
[ "$a" -eq "$b" ]
case $? in
0) printf '%s\n' "true" ;;
*) printf '%s\n' "false" ;;
esac