Live data from Hacker News

Pure Bash Bible (2018)

github.com

91–100 of 106 posts

Re: Pure Bash Bible (2018)

#91
post #72
post #35

Earlier quoted context omitted.

Just wait till you learn about: set -euo pipefail That should be line two of every bash script.

-eu? Yes. -o pipefail? Maybe not, for two reasons: 1. Pipefail is a bashism, and doesn't exist on Ubuntu's /bin/sh or Busybox /bin/sh (common on embedded Linux systems) 2. Pipefail invalidates some common shell patterns like "grep _something_ | xargs -r CMD". Pipefail is nice in some circumstances for sure. But it's probably not great advice to automatically copypasta into the top of every shell-script.

I don't agree on -e: see my comment higher up in the thread. It might be ok if you're hyper-aware, but recommending it to others will leave them baffled and confused in situations like that in my example where a non-zero exit is within the bounds of normal behavior of the script.

Re: Pure Bash Bible (2018)

#92

Earlier quoted context omitted.

Most commercial products that include an installer on *NIX are written in shell. No other method allows you to have a universal installer on every platform that will always work. > Especially for a company, unless you've got really, really skilled engineers on the scripts, it becomes a complete mess. And even then it can become almost impossible to grok. Dude. It's really not that hard a language. Is amateur shell sc…

No other method allows you to have a universal installer on every platform that will always work. No shell will guarantee that either. Last week I spent a few hours debugging an installer (bash script) that was failing with a rather puzzling error message (variable not defined). Non-Linux, bash 5.x. Worked on nearly everything under the sun including OSX with its ancient GPLv2 bash. Turns out on one non-Linux operati…

[deleted]

Re: Pure Bash Bible (2018)

#93
post #87

Earlier quoted context omitted.

At least you've discovered Zsh and hopefully its significant improvements over Bash. The only Bash benefit is readarray/mapfile, Z Shell is better in just about every other way.

Unless I'm misreading your comment, zsh does provide similar functionality via zsh/mapfile¹. It, and a lot of other cool stuff, is documented in zshmodules(1). ¹ https://zsh.sourceforge.io/Doc/Release/Zsh-Modules.html#The-...

I believe Bash readarray/mapfile is more like doing this in Zsh:

  lines=( "${(@f)$(my_command)}" )
or

  lines=( "${(@f)$(
which IMO is a little less nice than using a builtin with a name like "readarray".

But this is cool too.

Re: Pure Bash Bible (2018)

#94
post #37

I like bash, zsh and other shell languages as well. However, unless you're really experienced, they shouldn't be the product you ship, whether that is internally or externally to you or your company. Shell languages is the final layer of configuration and customization you apply to the setup, it should almost never be the foundation of what you build. Especially for a company, unless you've got really, really skilled…

Most commercial products that include an installer on *NIX are written in shell. No other method allows you to have a universal installer on every platform that will always work. > Especially for a company, unless you've got really, really skilled engineers on the scripts, it becomes a complete mess. And even then it can become almost impossible to grok. Dude. It's really not that hard a language. Is amateur shell sc…

It is probably the closest we're gonna get to a universal language, until we get wasm deeper in the os' or something. It is kind of the javascript of operating systems, which is both good and bad.

It is also quite bad that we've got to vet bash installation scripts before running them, as you may be required to run them as root. IMO, this isn't a huge issue for me personally, as we already are doing this for a lot of stuff, homebrew, linux package managers etc. We generally run a lot of untrusted code on our machines.

> Dude. It's really not that hard a language. Is amateur shell script ugly? verbose? redundant? bug-riddled? Absolutely. But it works despite that, and has fewer dependencies than other solutions, so in practice, it's fine.

Bash, zsh whatever, depend heavily on what is installed on the os, and what type of flavor of base tools are provided. Need base64 in a certain way, don't forget that line wrapping is different between bsd and linux. (a pain in the butt to test too).

Need to download something, better hope you're not running on a busybox or something with curl, or only have wget installed, now you gotta build support for both.

A lot of scripts, especially installation scripts are a product of blood, sweat and tears, mostly tears tho. Each line can be interpreted in a multitude of ways, under different conditions and versions of bash, os and so on, and the script will have to be robust enough for that.

> That said, use whatever you know. Wanna use Python? Use it. Wanna use a compiled language? Do it. Just make something. The biggest difficulties you will have in producing a product will not be the language.

It may not be the biggest difficulty, but it certainly is something we should consider. We've chosen our values, and shell scripts doesn't align with those, I'd rather not fight my environment every step of our development journey.

I am not building a new product, I am maintaining and expanding a product, as such I am heavily biased towards getting as few support tickets as humanly possible, while building an awesome experience. We are also mostly software engineers, and not sysadmins, as such we prefer compiled binaries. We then choose to distribute them through curated and secure channels.

We get the benefit that we can actually test parts of our tools in isolation, benchmark them if necessary, and version our stuff in a nice concise way. If I want to run our onboarding installation, I can simply fetch our current list of binaries expected for developers and run it, because I know they don't depend on external tools from the environment. This is a lot of work, but in a large development organization, this means a lot.

> You don't have to avoid shell just because people on HN (who've probably never read the man page) keep repeating that it's terrible. With Shellcheck, almost anyone can write good scripts now. If you can write a concise, bug-free script that works, then do it.

Just to reiterate, I am not digging on shell scripts, and I don't necessarily think they're terrible, they have their time and place, and for us, it is customization and extensibility. We setup our tools such that they work awesome on the cli, and can be composed however, developers like, but that is up to them, and how they want to form their workflows.

We've seen that developers really struggle to maintain what was previously built (myself included), so now we've got a more homogeneous tech stack, which comes with its own trade offs, but whose values align more with our engineering culture.

Re: Pure Bash Bible (2018)

#95

Earlier quoted context omitted.

Most commercial products that include an installer on *NIX are written in shell. No other method allows you to have a universal installer on every platform that will always work. > Especially for a company, unless you've got really, really skilled engineers on the scripts, it becomes a complete mess. And even then it can become almost impossible to grok. Dude. It's really not that hard a language. Is amateur shell sc…

No other method allows you to have a universal installer on every platform that will always work. No shell will guarantee that either. Last week I spent a few hours debugging an installer (bash script) that was failing with a rather puzzling error message (variable not defined). Non-Linux, bash 5.x. Worked on nearly everything under the sun including OSX with its ancient GPLv2 bash. Turns out on one non-Linux operati…

This seems to be a general problem for what I normally refer to orchestration software. I.e. calling a lot of external dependencies, io and in general very slim on business logic. You've simply got to deal with too many dependencies, which may or may not have different versions, flavors and whatnot. It becomes impossible to test every permutation, so instead you rely on brute force to weed out bugs, simply fix the things once a bug report comes in.

It helps having good integration/e2e tests, but they only go so far, customers/developers customize and mess up their systems all the time.

Which is why I've choose binaries instead, they are mostly self-contained (except for various things about gnu and musl), and in general just rely on a higher level of built in functionality (because of the statically linked nature).

Re: Pure Bash Bible (2018)

#96

Earlier quoted context omitted.

> Bash doesn't even support floating point! But bc (and dc) does, and shell authors deemed this was good enough. And that's part of the problem with the "Unix philosophy"

Yeah every invocation of bc is a call to a command. So if you do it in a loop, its going to be very slow. I had to do this once. It was so slow, I decided to use integers in kind of fixed-point math where I just multiply all the floats by 100 and work with ints. zsh supports float though

Did you consider preparing the arguments in the loop then run them all at once?

Re: Pure Bash Bible (2018)

#97

Earlier quoted context omitted.

What could go wrong? Use a technology that is notorious for outputting wrong information to write a script that is really hard to get it right and then later reviewed by someone that can’t really understand it. At this point just use something else.

You say that likes writing code doesn’t involve experimentation in general. You can’t trust the code that that Chatgpt gives you, but you also can’t trust the code that even you write until you test it. It is always an iterative process. Why does it matter that some use AI tools as a starting point?

You can do a lot of irreversible damage with the shell, if you're unsure of how a line will behave don't run it.

Re: Pure Bash Bible (2018)

#98
post #83
post #52

Earlier quoted context omitted.

For 1-liners there are only 2 real non-shell contenders - Perl and Ruby. Yes you can do them with Python but it ain't pretty.

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.

Re: Pure Bash Bible (2018)

#99
post #66

Earlier quoted context omitted.

One major reason that Debian chose dash is plainly written on the bash manual page: $ man bash | sed -n '/BUGS/,/^$/p' BUGS It's too big and too slow. Another major reason is adherence to the POSIX standard; bash was written a decade prior to standardization, and has interesting issues because of this. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...

> bash was written a decade prior to standardization, and has interesting issues because of this. bash can do whatever it wants to do if it's called as "bash". But it should only do specific things when called as "sh". This is true of any shell: > When interpreted with dash instead of bash, the same script will fail. This is because dash is much stricter than bash in following the sh standard. Since dash is designed…

One interesting expression of the bash personality disorder is alias.

If you alias p=printf in a #!/bin/sh script, it will work; it will fail in a #/bin/bash script, unless POSIX mode is forced.

Re: Pure Bash Bible (2018)

#100

I'd love to see a little language which compiles to _readable_ bash. Could it be so hard? There's a dead project that comes up now and then called bish. https://github.com/tdenniston/bish

Write the script in pseudo python and ask ChatGPT to convert it into bash but leave a lot of comments
Post reply on HN