5 ms·
That's fine. But simple things that should work in order to be painless, don't. -- -- bash:~]$ if $var < 21; then echo "smaller"; fi bash: 21: No such file or
by salamander014 6y ago
That's fine. But simple things that should work in order to be painless, don't.
--
--
bash:~]$ if $var < 21; then echo "smaller"; fi
bash: 21: No such file or directory
--
--
BUT this works:
if (( $var < 21 )); then echo "smaller"; fi
smaller
--
--
BUT this DOESN'T:
if [[$var < 21 ]]; then echo "smaller"; fi
bash: 21: No such file or directory
--
--
BUT this DOES (added a space after '[['):
if [[ $var < 21 ]]; then echo "smaller"; fi
smaller
Plus the fact that having to type out 'then' and 'fi' and that semi-colons are semi-necessary are annoying. Compared to today's languages, very little of the non-posix parts of bash feel well though out. We can do better.
All these things make it necessary to understand bash. Most modern programming languages feel familiar. Bash and Sh don't anymore. They feel antiquated.
- cduzz 6y agoI'm not defending shell, just pointing out how it works. One of my biggest annoyances with shell is that there are these magic programs such as [ that pretend to be part of the language but aren't. If if $var has an exit status (because perhaps you insanely want to have var be some program set elsewhere) your if statement would work.... These other examples have operators (test or similar expanded builtins), and those set the exist status that the if block reads. so grep -q hacker < /etc/passwd if [ $? -eq 0 ] ; then echo hackd! ; fi is an indirect way of saying: if grep -q hacker < /etc/passwd ; then echo hackd! ; fi Shell is terrible, but the way people use it makes it even worse because the sugar hides what's actually going on.
- forgotpwd16 6y agoMeanwhile in zsh $ if (( $var < 21 )); then echo "smaller"; fi smaller $ if [[ $var < 21 ]]; then echo "smaller"; fi smaller $ if [[ $var < 21 ]] { echo "smaller" } smaller