News for hackers. Stuff that matters.
Re:Pure sh bible – Posix sh alternatives to external processes by nevertoolate on Monday October 19, 2020 @02:10PM (#24829083)
why the bash hate? I thought it was really uncommon for any unixlike os to come without bash (sh symlinked to bash)
Re:Pure sh bible – Posix sh alternatives to external processes by patrec on Monday October 19, 2020 @03:47PM (#24830159)
Linux in practice always comes with bash, but sh is not necessarily an alias; e.g. on the world's most popular distro:

    > readlink -f /bin/sh
    /usr/bin/dash
Also, BSDs and derivatives either have no bash installed by default (e.g. OpenBSD) or, in the case of macOS a horribly outdated one.

Finally there are good reasons to hate bash: Bourne shell is flawed but brilliant, whereas bash is a bloated mess of unpredictable and counter-intuitive behaviour that also tends to change between versions. It's also much, much slower than e.g. dash. Unfortunately even for pure shell scripting bash in practice is the way to go; posix shell (and dash) unfortunately lack a few things that are pretty crucial and very cumbersome to work around, like process substitution.

Re:Pure sh bible – Posix sh alternatives to external processes by CDRdude on Monday October 19, 2020 @04:31PM (#24830661)

    > or, in the case of macOS a horribly outdated one.
The latest version of macOS defaults to zsh instead of the outdated version of bash.
Re:Pure sh bible – Posix sh alternatives to external processes by moondev on Monday October 19, 2020 @07:09PM (#24832060)
Doesn't matter when shebang is #!/bin/bash
Re:Pure sh bible – Posix sh alternatives to external processes by lilyball on Monday October 19, 2020 @06:52PM (#24831909)
That's just for the login shell. It still ships with /bin/bash, and /bin/sh still by default invokes bash (in sh compatibility mode). However there is a systemwide flag you can set to make /bin/sh run dash or zsh instead.
Re:Pure sh bible – Posix sh alternatives to external processes by patrec on Monday October 19, 2020 @05:42PM (#24831344)
That's mostly irrelevant, since basically no one writes shell code in zsh and this is unlikely to change that.
Re:Pure sh bible – Posix sh alternatives to external processes by ByteJockey on Monday October 19, 2020 @10:59PM (#24833547)
That's probably going to change with macs having it as the default.

Never underestimate the power of "Well, it worked on my machine"

People seem to be willing to do apt-gets (or equivalent) in docker files, I could see several of my old co-workers doing that just to get a script working.

Re:Pure sh bible – Posix sh alternatives to external processes by patrec on Tuesday October 20, 2020 @03:32AM (#24834739)
Unlikely. Apple have made the default interactive shell zsh, but /bin/bash still stays around and I believe /bin/sh still aliases to it by default. Meanwhile there is an enormous ecosystems of shell scripts including countless curl ... | bash installers which no one has any motivation to port to some shell which is not installed by default on most Linux distros. Although Microsoft would undoubtedly be delighted with Apple accelerating the move of developers to Windows as the Unix of choice, I'm not convinced even Apple's current management is going to be dumb enough to remove bash altogether.
Re:Pure sh bible – Posix sh alternatives to external processes by wott on Monday October 19, 2020 @02:25PM (#24829273)
Why speak of hate?

The main problem I have with bashisms is that most people who use them just assume everyone runs bash as default shell. Instead of specifying xxx/bin/bash in their script, they use xxx/bin/sh (and they don't run any check about which kind of shell is running to ensure that bash is running and fail gracefully otherwise, and they don't either document that the script must be run from bash).

There's no problem about writing in a specific language, if you clearly say you did. If I stretch the example a bit, I wouldn't like a Python script to start with xxx/bin/ruby.

Re:Pure sh bible – Posix sh alternatives to external processes by klodolph on Monday October 19, 2020 @03:29PM (#24829954)
There is absolutely no Bash hate here.

If you do an experiment you'll easily find that dash is faster than bash, and since dash basically just a bare POSIX shell with very few extensions, a script that runs in dash is more likely to run anywhere (ignoring the fact that nearly every program you call from a shell script comes in both a GNU and BSD variant, and possibly a more minimalist POSIX version).

So I write my shell scripts to dash, which is /bin/sh on my system (Debian).

If you use Bash-isms, you should definitely not put #!/bin/sh at the top of your script, you should be putting #!/bin/bash

Re:Pure sh bible – Posix sh alternatives to external processes by eikenberry on Monday October 19, 2020 @02:55PM (#24829621)
Just because it is present, doesn't mean its the default or used internally by things that call shell scripts.

For example... Not that long ago I started a new job where I took over as maintainer of a project with a fairly extensive Makefile, including targets for release. I cleaned it up some but missed a couple bashisms. The original author wrote it on a Mac, on which /bin/sh is bash, and used bashisms. I took over on Linux, which has /bin/sh as dash (POSIX). So while the author used it fine, it broke for me in subtle ways that I didn't notice until I made my first, broken release (error was in formatting of md5 signature file).

The problem with bashisms is that people don't think through if their current use case is an appropriate one.

Re:Pure sh bible – Posix sh alternatives to external processes by unphased on Wednesday October 21, 2020 @11:28AM (#24848777)
just change the shebang
Re:Pure sh bible – Posix sh alternatives to external processes by josephcsible on Monday October 19, 2020 @02:25PM (#24829291)
It's not bash hate. It's that a lot of the time, you don't have bash available (e.g., BusyBox environments), and you want your scripts to still work.
Re:Pure sh bible – Posix sh alternatives to external processes by matvore on Monday October 19, 2020 @02:23PM (#24829250)
The `/bin/bash` on macOS is severely out of date. So a script that starts with `#!/bin/bash` will be unable to use a lot of features.

If you only rely on `/bin/sh` the user has the flexibility to point `/bin/sh` to whatever shell on their system is fastest and most up-to-date (e.g. `/bin/dash` is a pretty good choice for performance).

Re:Pure sh bible – Posix sh alternatives to external processes by dazzawazza on Monday October 19, 2020 @03:23PM (#24829879)
None of my FreeBSD machines have bash installed because it's not part of FreeBSD.
Re:Pure sh bible – Posix sh alternatives to external processes by aidenn0 on Monday October 19, 2020 @02:13PM (#24829121)
I don't know if it still is, but for a decent while, Debian sh was not bash.
Re:Pure sh bible – Posix sh alternatives to external processes by voidfunc on Monday October 19, 2020 @04:08PM (#24830390)
Debian uses dash for non-interactive shell, but bash for the interactive shell (if I recall).
Re:Pure sh bible – Posix sh alternatives to external processes by aidenn0 on Monday October 19, 2020 @06:34PM (#24831780)
Indeed /bin/sh is still dash, not bash.
Re:Pure sh bible – Posix sh alternatives to external processes by 1vuio0pswjnm7 on Monday October 19, 2020 @03:45PM (#24830126)
If someone asked you to list all the UNIX-like OS you know about, would BSD or Plan9 make the list.