Hacker News new | ask | show | jobs
by _where 2082 days ago
If it’s not by default there’s a reason. Bash is literally running commands in a shell session. Think terminal session. When a command fails, would you want the terminal session to end? That’d be annoying.

Same theory for unset variable. Referencing an undefined variable shouldn’t break your session. Why initialize it anyway? It’s more code to change if you don’t use it if you have to initialize it when it might not be needed. And, you’d have to call the script with A= just to check A wasn’t defined, and in the process now you have A assigned to an empty string, instead of only defaulting to one when called, which uses more memory and execution time.

The pipeline doesn’t die because && and || and parens are seriously helpful for one-liners.

Don’t think of it as a script. Think of it as a script for a shell.

4 comments

Thinking of it that way, it becomes clear why you would want to switch to something like Python for more complex workflows.

Bash is brilliantly useful for a lot of things. I’m not bashing it.

> you’d have to call the script with A= just to check A wasn’t defined, and in the process now you have A assigned to an empty string

You have ${var:-} for doing that

Sometimes the only reason is backwards compatibility.
A compelling argument for why Bash scripts shouldn't exist at all.
Ah yes the "nothing can ever change" argument. It's the reason we still code everything in FORTRAN and COBOL.
http://www.oilshell.org/blog/2018/01/28.html#im-still-angry-...

The fallacy is: "By posting comments on the Internet, I can prevent people from using a tool I'm frustrated with. And I can make millions of lines of existing programs that I rely on go away."