4 ms·
> And now for the final lolz. I’ve said above that these are the commands you use to evaluate expressions… but the shell also has expressions of its own via the
by shmerl 3y ago
> And now for the final lolz. I’ve said above that these are the commands you use to evaluate expressions… but the shell also has expressions of its own via the !, &&, and || operators—all of which work on command exit statuses.
It works for simulating very basic boolean expressions, but it gets ugly quickly if you need some more complex combos of NOT, OR and AND. I wish Bash had proper boolean expressions support.
- remram 3y agoWhat do you mean? Bash does have (), {}, !, &&, || What else do you need for "proper Boolean expressions support"?
- shmerl 3y agoI 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.
- candiddevmike 3y agoIt works fine, you can satisfy your boolean logic needs with bash tests and exit codes. You'd be amazed at how much of the world runs (just fine!) on bash conditionals.
- hyperhopper 3y agoYou'd be surprised how much of the world runs on cobol and Fortran. Doesn't mean those are good or even okay or that people should choose to use them.
- Annatar 3y agoModern post-2012 Fortran is excellent, and the compiler produces very fast code, GPU or otherwise. And it is very portable.
- deleted 3y ago[deleted]