3 ms·
Hello fellow traveler. I've got 8 years of scripts with `test` in them to prove I agree with you. It's a lonely road out there... I blame the Google shell style
by chatmasta 3y ago
Hello fellow traveler. I've got 8 years of scripts with `test` in them to prove I agree with you. It's a lonely road out there... I blame the Google shell style guide.
I picked up the habit of preferring `test` when I was writing scripts that needed to run in both `sh` and `bash`, but I kept it because it makes more semantic sense to me than treating a character like `[` as a command. It's also weird that `]` is an argument to `[` rather than also being a binary. I mean, I understand the technical reasoning... but it feels like a hack.
- js2 3y agoHello fellow traveler. I've got 20 years of scripts with `if test` in them. I only use `[[` when I need functionality it provides that `test` does not (pattern or regex matching, typically, and for pattern matching I'll generally use a case statement instead).
- chatmasta 3y agoYeah that's when I use it too. I figure if I've already given into the temptations of Bash, I may as well go all the way. Sometimes if I'm feeling extra frisky I even do [[ .. ]] || { echo "error!" ; exit 1 }
- cduzz 3y agoI imagine most people have sets of macros they habitually add to any sufficiently complex script they're modifying... yelp(){ es=$1 shift echo "$@" >&2 exit $es } test -d /tmp/goober || yelp 33 "couldn't find goober!" (yes, I'm sure there are standards for exit status ranges and 33 is not such a thing)
- quicklime 3y agoThe Google style guide doesn’t disagree with you, it says to code exclusively for bash where possible (ie mostly everywhere inside Google) and to prefer [[ over [ and test: https://google.github.io/styleguide/shellguide.html#s6.3-tests https://google.github.io/styleguide/shellguide.html#s6.3-tes... In a scenario where you also need to support shells other than bash, Google’s shell style guide doesn’t say to use [ over test.