3 ms·
But you can do foo=0 # true bar=1 # false [ $foo = 0 ] && [ $bar = 0 ] baz=$? echo $baz The syntax is a bit unusual coming from more mode
by account42 3y ago
But you can do
foo=0 # true
bar=1 # false
[ $foo = 0 ] && [ $bar = 0 ]
baz=$?
echo $baz
The syntax is a bit unusual coming from more modern languages, as is the use of 0 as true. But you can express whatever conditionals you want. Remember that C originally had no dedicated boolean type either.
You could also use numerical expressions although I don't recommend it due to being even less readable (even more so if you have expression complex enough that you need to protect against overflow):
baz=$((foo + bar)) // foo && bar
baz=$((foo * bar)) // foo || bar
- shmerl 3y agoThat still doesn't make it look nice, since if you use false / true, you can use variables directly in if checks. For example: foo=true if $foo; then echo yes fi But with 0 / 1 - that won't work. I.e. you can get some, but not all features of a normal boolean type in various ways.