Live data from Hacker News

Pure Bash Bible (2018)

github.com

101–106 of 106 posts

Re: Pure Bash Bible (2018)

#101

Earlier quoted context omitted.

Do you have any specific examples to cite? The snippets are meant to be run in POSIX-compliant shells like dash/yash/ash/etc, not just Bash in compatibility mode. In fact, there used to be a section which listed workarounds[1] for bugs in dash, but they have since been fixed[2]. If you are still using an old version of dash, you may need to use them. [1] https://github.com/dylanaraps/pure-sh-bible/commit/70f410ebb...…

https://news.ycombinator.com/item?id=35768615 Perhaps dash now has these features but does NetBSD sh or FreeBSD sh have them. What's the point of "pure sh" if it's restricted to specific versions of shells. Opinions may differ but I'd rather learn the "lowest common denominator". I use the same scripts on both Linux and BSD so I need portability. I do not use bash for scripting. It's larger and slower. One source I c…

> does NetBSD sh or FreeBSD sh have them.

Yes and yes. My FreeBSD machine has not even been updated in 5 years, if that helps.

> What's the point of "pure sh" if it's restricted to specific versions of shells.

The aforementioned features have been implemented for a very long time. The issues with old versions of dash I mentioned were straight-up crashes for very simple things, not some fancy new-fangled feature that was yet to be implemented. Even then, the bible specifically listed workarounds for them.

dylanaraps has quite a prolific collection of shell programs, and they run on a variety of operating systems. Most notably, pfetch[1] runs on Linux, Android, NetBSD, FreeBSD, OpenBSD, Minix, Haiku, macOS, Solaris and IRIX. I assure you that he is fully aware of the importance of compatibility.

His pure Bash bible even has very thorough warnings for Bash versions required, since macOS uses Bash 3.2 (released in 2006).

[1] https://github.com/dylanaraps/pfetch

Re: Pure Bash Bible (2018)

#102

Earlier quoted context omitted.

Do people actually symlink /bin/dash to /bin/sh? That would be extremely stupid.

> That would be extremely stupid. Why?

Because if you are not 100% compatible with bourne shell you shouldn't automatically execute script made for bourne shell.

I have no problem with people using #!/bin/dash shebang for their scripts and using dash as their default shell but it shouldn't pass itself as something else. It is fine with bash because it is bourne shell compatible.

Re: Pure Bash Bible (2018)

#103

After years writing bash scripts I stumbled with this line... set -e Which forces the script to exit on first error. This line be in the bible as Genesis 1:1

you could also just write as if you're writing go:

function main {

  if ! failing_cmd; then
    log "this fails!"
    return 1
  fi

  log "this won't log"
}

main

I only mention this because there are folks who don't like set -e.

Re: Pure Bash Bible (2018)

#104

Earlier quoted context omitted.

> That would be extremely stupid. Why?

Because if you are not 100% compatible with bourne shell you shouldn't automatically execute script made for bourne shell. I have no problem with people using #!/bin/dash shebang for their scripts and using dash as their default shell but it shouldn't pass itself as something else. It is fine with bash because it is bourne shell compatible.

> Because if you are not 100% compatible with bourne shell you shouldn't automatically execute script made for bourne shell.

The same can be said about bash.

> It is fine with bash because it is bourne shell compatible.

I was running an HPC cluster when we upgraded Debian during the bash->dash change, and there were all sorts of problems: IIRC most folks simply changed their scripts from /bin/sh to /bin/bash.

Re: Pure Bash Bible (2018)

#105
post #98
post #83

Earlier quoted context omitted.

One-liners are meant to be thrown away, so it does not matter if it's pretty or not. If you care enough to keep it, you should consider rewriting it for clarity. That last step does not require using any particular language; e.g. my PS1 started off as an increasingly convoluted one-liner, until I rewrote it in Go.

PS1 in Go? Overkill, doesn't come close.

Depends on what you're trying to do. If you're shelling out to git(1) or docker(1), rather than e.g. recursively checking for the presence of .git in parent directories, or inspecting ~/.docker/config.json, then the fork+exec overhead is already quite significant. Next if you're parsing ~/.docker/config.json in shell, you're most likely either asking for trouble or (again) shelling out to jq. Writing it all in an interpreted language means you're paying the cost of interpreter startup, which on underpowered systems can take hundreds of milliseconds even when idle. OTOH loading a static binary to memory happens only once, and with Go you can trivially cross-compile.

I also have a fallback shell one-liner, without any of the fanciness like displaying the current git branch:

https://github.com/rollcat/etc/tree/master/cmd/prompter#i-li...

Re: Pure Bash Bible (2018)

#106

Earlier quoted context omitted.

Do you have any specific examples to cite? The snippets are meant to be run in POSIX-compliant shells like dash/yash/ash/etc, not just Bash in compatibility mode. In fact, there used to be a section which listed workarounds[1] for bugs in dash, but they have since been fixed[2]. If you are still using an old version of dash, you may need to use them. [1] https://github.com/dylanaraps/pure-sh-bible/commit/70f410ebb...…

https://news.ycombinator.com/item?id=35768615 Perhaps dash now has these features but does NetBSD sh or FreeBSD sh have them. What's the point of "pure sh" if it's restricted to specific versions of shells. Opinions may differ but I'd rather learn the "lowest common denominator". I use the same scripts on both Linux and BSD so I need portability. I do not use bash for scripting. It's larger and slower. One source I c…

There are people commenting on HN who do not understand the difference between compatibiity and portability. This doesn't stop them from commenting on someone else's preference for portability.
Post reply on HN