5 ms·
Conditional statements are even harder to remember. How many square brackets do I need? 1 or 2? Do I use == or -eq? Where does the semicolon go again?
by lexandstuff 4y ago
Conditional statements are even harder to remember. How many square brackets do I need? 1 or 2? Do I use == or -eq? Where does the semicolon go again?
- pas 4y ago[ is an alias for test, a very simple program [[ is the built-in this means you need to write everything separate, test doesn't parse, plus it needs the closing bracket if you use the alias but if-then works with any expression, and you put ; after expressions if test 1 -eq 2 ; then echo "oh noes" ; fi
- duped 4y agoSee that's even stupider than just having syntax for it
- cassepipe 4y agoI somehow already read that in the past but here, in this condensed form, it now actually makes sense. I think you just cured me of my bash fear. Thanks for that explanation. I'll make sure to either use test or [[ and never [ again so that I remember the difference. And I am currently reading man test
- pas 4y agoI think using test would be the best, even if it's awkward, but [[ is a Bashism (so not POSIX shell compatible), and [ is jut an alias for test and requires that dumb closing bracket, and you are already execve()-ing a separate program for comparison, you are already depending on the exit code, so why not make it explicit by using test. (I continue to use [[ and never look back, just forward, trying to picture the shining city on a conveniently sloped hill, where people live without fear in their hearts, where they have compile time checking for their scripts, and the compiler gently pushes them toward sane error handling. Not too much, but definitely not too little. Something like Rust's Result type.)
- sigjuice 4y agohttps://twitter.com/jakewharton/status/1334177665356587008 https://twitter.com/jakewharton/status/1334177665356587008
- chlorion 4y agoIn bash or bash-like shells you always use the double square bracket tests! They support everything the regular test syntax does, but have some extra features that make them "safer" to use. Some code style guides for Linux distros or work places like Google actually enforce this!
- chriswarbo 4y ago> How many square brackets do I need? 1 or 2? 2 > Do I use == or -eq? For string comparison, use '='. For arithmetic comparison, use -eq, -gt, -lt, etc. > Where does the semicolon go again? Whenever you're putting multiple lines on one. If you use multiple lines, you don't need semicolons. For example: if [[ "$result" = 'SUCCESS' ]] && [[ "$count" -gt 0 ]] then echo 'Hooray!' fi
- mcluck 4y agoPeople complain about === in JS but at least I can just use it everywhere and stop using ==. Needing to remember which equality check to use based on type is so frustrating
- 0xbadcafebee 4y agoType man bash. Go to section "SHELL GRAMMAR". Go down to "Compound Commands". Then go read the section "CONDITIONAL EXPRESSIONS". If you want a much-reduced version, read man dash instead. It is very small and close to POSIX semantics. You can then later go find the Bashisms.