4 ms·
That's what shebangs are for: #!/usr/bin/env bash If it's not there, it will fail. I expect every script with `#!/bin/sh` as shebang to be POSIX shell only
by linkdd 6y ago
That's what shebangs are for:
#!/usr/bin/env bash
If it's not there, it will fail.
I expect every script with `#!/bin/sh` as shebang to be POSIX shell only.
- dataflow 6y ago> If it's not there, it will fail. Until your users/colleagues source it into another shell, or copy-paste some part of it into their shell/Makefile/Bazel/other script... and then either you feel sorry for them, or you laugh at them and blame them for burning themselves and trashing their system. (Hopefully your choice didn't cause their files to get wiped. Unless you think they deserved it for daring to paste your code somewhere else, I guess.) The world isn't perfect.
- linkdd 6y agoThis problem is not exclusive to bash, try copy-pasting python 2 code into python 3. Or ES2020 code in a browser. Or ASM z80 into an x86 program. When you're copy-pasting, you should be aware of what you're copying, and where you're pasting it. Surely, no domain in IT is idiot-proof.
- dataflow 6y ago> This problem is not exclusive to bash, try copy-pasting python 2 code into python 3. No, it is highly exclusive to Bash actually. In Bash, even one-character mistakes have a high potential to cause catastrophic damage. Just splitting or globbing on the wrong character can wipe your filesystem. As well as issues like these, where your string being '!' suddenly causes the command to be interpreted entirely differently. And Bash has modes that keep going when there is an error so it's not like you'll even know you've encountered an error or get any kind of visible crash + stack trace necessarily. It just keeps trashing everything further and further. These are extremely wildly different from Python. > Surely, no domain in IT is idiot-proof. I would love to know what happens when you say this to a user who pastes your Bash in zsh. Do you tell them your code isn't idiot-proof? Is everyone who ends up running Bash in zsh for some reason or another an idiot?
- linkdd 6y agoIf you're not doing `set -euo` and if you run your scripts as root before testing them, you're asking for something wrong to happen. EDIT in response to your EDIT: My users get either: - a tar.gz archive - a binary - a docker image - an helm chart - a POSIX shell script (very rare) Then they can use any method they want to deploy it. You want your software to be portable? You have to put in the work to do it. If you expect your users to know better (assume they ARE idiots), again, you're asking for something wrong to happen.
- DonHopkins 6y agoNo, if you're using a language that requires you to perform some obscure incantation like "set -euo" before running your scripts, you're asking for something wrong to happen. Blame the disease, not the symptom.
- dylan604 6y agoif your pedantic enough to use "set -euo", you're going to add it to the script and not make a user set it first.
- dataflow 6y ago> If you're not doing `set -euo` and if you run your scripts as root before testing them, you're asking for something wrong to happen. ...because you can't delete important files unless you're root, right? Subfolders in your home directory are totally OK being deleted? You store your documents so that only root can modify them? And do you know set -e has a ton of its own pitfalls and doesn't save you from this? It's a half-broken mitigation. I don't have energy to keep arguing here, but I would suggest go learning about the pitfalls of Bash and set -e and such.
- dolmen 6y agoYou mean "set -euo pipefail", don't you? You should use more copy/paste. ;)
- acdha 6y agoIf people copy-and-paste complex shell scripts without testing, you’re going to have a bad day no matter what you do. The solution is to work on better practice, use of shellcheck, rewriting complex logic in Python, etc. since that’ll pay dividends in so many other ways.
- dolmen 6y ago> Until your users/colleagues source it into another shell The intersection of people knowing the "source" command and not knowing what is a she-bang or the difference between sh and bash is quite small.
- dataflow 6y agoSo what is your point here? I'm hallucinating when I see people do this?
- cjaybo 6y agoAre you suggesting that leaving off the shebang line makes these types of mistakes less likely? That makes no sense.
- skinner927 6y agoIf I copy Python 2 code into Python 3 it’s not gonna work right either. What’s your point?