Re:Type-ish – A runtime type checker for bash, in bash
by EdSchouten on Sunday June 19, 2022 @08:10AM
(#31798571)
Booleans in shell scripts are little more than zero (true) and non-zero (false) exit codes.
Re:Type-ish – A runtime type checker for bash, in bash
by MauranKilom on Sunday June 19, 2022 @08:28AM
(#31798678)
Ok, right. But then it should still say either "booleans are just numbers" or "numbers can be interpreted as booleans, i.e. are truthy or falsy".
Re:Type-ish – A runtime type checker for bash, in bash
by ancientsofmumu on Sunday June 19, 2022 @11:11AM
(#31800122)
It's not that cut & dry, as bash is doing evaluations; 0 and 1 can both be "true" if simply used without an evaluation, but add logic and it starts to matter (grep returns 0 if hit, 1 if not).
[[ 0 ]] && echo "hit 0"
[[ 1 ]] && echo "hit 1"
grep -q 127 /etc/hosts && echo "hit grep"
grep -q 128 /etc/hosts || echo "miss grep"
> [...]
> boolean: a shortcut for returning a number (as numbers are booleans).
Can someone explain? I can understand booleans being numbers (e.g. #define TRUE 1 in C), but how are numbers booleans in bash?