3 ms·
Can you explain why?
by francis-io 9y ago
Can you explain why?
- Sir_Cmpwn 9y agoFor portability. bash is not available everywhere, but POSIX sh is a requirement of POSIX so you can expect it to be there.
- yjftsjthsd-h 9y agoExactly: GNU bash is on most GNU/Linux systems, but: * mac OS uses an ancient version of bash * Embedded systems use busybox sh * Android uses its own version of sh * None of the BSDs come with bash, since, y'know GNU licensing EDIT: Formatting
- davewongillies 9y agoAnd to make things interesting Ubuntu uses dash as sh https://wiki.ubuntu.com/DashAsBinSh https://wiki.ubuntu.com/DashAsBinSh
- OJFord 9y ago`sh` is only supposed to be _a_ POSIX-compliant shell, it's not odd/interesting/confusing that Ubuntu uses dash any more than it is that something else uses bash. The shebang line #!/bin/sh should only be used for POSIX-compliant scripts; if it needs {bash, dash, python, etc.} it should start #!/usr/bin/env {bash, dash, python, etc.}.
- yjftsjthsd-h 9y agoIn fairness, that's just for sh; if for some reason you actually need bash you can just use `#!/usr/bin/env bash`
- mjevans 9y agoIf you actually //need// bash ask for it, not sh. This comes from more armature script authors not being precise enough with requirements, or not targeting the more common and portable spec.
- skj 9y agoNot all systems have bash.
- v_lisivka 9y agoNot all systems have POSIX, perl, python, javascript, C++, rust, go, etc., so should we write all our programs and scripts in sh?
- Goladus 9y agoThe relevant point is that all systems running bash should also be POSIX compliant. So, taking some care to avoid bash-only constructs in a shell script can reap substantial gains in portability at fairly minor cost.
- v_lisivka 9y agoWhy I should care about all systems? If targeted systems have bash4.x and GNU tools, why I should write scripts in sh? If you care about all systems, ANSI C is better choice, IMHO.
- Goladus 9y agoIf you're targeting a limited set of systems and can reasonably assume the requirements won't change in the future, obviously use whatever tools you know will be available. I write Bash and Python all the time for those reasons. But it's silly to think that there's nothing in between "complete control over target environment" and "use ANSI C so it can be compiled to any architecture and platform under the Sun." POSIX compliance will cover a lot. Try browsing /usr/bin on any Unix system you'll see plenty of use cases. I just looked at /usr/bin on my laptop and saw that mysqld_safe is a Bourne shell script, for just one example.
- wirrbel 9y agoless magic (which you start to care for if you have to use other people's magic), easier to port stuff to zsh, etc.