News for hackers. Stuff that matters.
Re:Shell Script Best Practices, from a decade of scripting t... by Cerium on Thursday October 27, 2022 @09:49AM (#33357429)
"Use [[ ]] for conditions"

Oh how I hate the double square bracket. It is the source of many head scratching bugs and time wasted. "The script works in my machine!" It doesn't work in production where we only have sh. It won't exit due to an error, the if statement will gobble the error. You only find the bug after enough bug reports hit that particular condition.

After a couple shots to the foot I avoid double square brackets at all cost.

Re:Shell Script Best Practices, from a decade of scripting t... by kdmccormick on Thursday October 27, 2022 @10:58AM (#33358520)
If I may ask, why do you only have sh in production?
Re:Shell Script Best Practices, from a decade of scripting t... by Cpoll on Thursday October 27, 2022 @11:07AM (#33358671)
A common containerization philosophy is to use a bare-minimum base image and add only what you need. Something like an Alpine container doesn't come with Bash.
Re:Shell Script Best Practices, from a decade of scripting t... by 0xbadcafebee on Thursday October 27, 2022 @11:08AM (#33358685)
Alpine docker container only comes with ash shell by default. If you don't use any Bash-isms, you can just write a POSIX shell script. Otherwise if you have many containers from many different sources, you might have to bake all new containers just to add Bash to them.
Re:Shell Script Best Practices, from a decade of scripting t... by Cerium on Thursday October 27, 2022 @11:09AM (#33358698)
In this case production is a fleet of embedded devices.
Re:Shell Script Best Practices, from a decade of scripting t... by therealjumbo on Thursday October 27, 2022 @11:01AM (#33358576)
This should be fixed with a shebang and shellcheck. If your shebang is #!/bin/sh, shellcheck will complain loudly about bash-isms. If production is sh and doesn't have bash, there's quite a few other bash-ism you want to check for. You can run shellcheck in CI to check your scripts and return non-zero if they aren't clean, and you can force off warnings for lines that are ok.

EDIT: I should have said, "could be fixed once and for all", "should" is just my opinion.