3 ms·
I don't think you can assign boolean variables to boolean expressions, becasue there is no boolean type. Something like this won't work to become false: f
by shmerl 3y ago
I don't think you can assign boolean variables to boolean expressions, becasue there is no boolean type.
Something like this won't work to become false:
foo=true
bar=false
baz=$foo && $bar
echo $baz
Or even simply:
foo=! $foo
With numeric expressions, you can at least use $((...))
- emmelaich 3y agoYeh you'd have to do something like foo=true bar=false $($foo) && $($bar); baz=$? case $baz in 1) echo false;; 0) echo true;; esac It just ain't worth it.
- shmerl 3y agoYeah, that's very convoluted. It would be useful to have some context for boolean expressions, similarly how $((...)) works for numeric ones.
- account42 3y agoBut 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.