set -e
Which forces the script to exit on first error.
This line be in the bible as Genesis 1:1
set -e
Which forces the script to exit on first error.
This line be in the bible as Genesis 1:1
That should be line two of every bash script.
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 wasn't suggesting for a generic posix shell. It's explicitly for bash.
If you're going to dumb down to pure posix, it's even more painful as you can't even have local function variables.
> 2. Pipefail invalidates some common shell patterns like "grep _something_ | xargs -r CMD".
That type of situation is the exception, not the rule. The more common situation is "foo some-input.txt | bar" and you want it to fail if foo fails.
> 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'd much rather have that type of failure than an inadvertent continuation of a script when something early in a pipeline failed.
I like JavaScript. Because I'm good enough at it to ignore the bad parts. If I get good enough at bash, will it get better?
Bash itself will not get better because you'll be stuck writing scripts targeting some ancient version to ensure wider compatibility.
It's still a powerful skill to develop. It's a sweet spot between a one-off shell pipeline and something that involves actual logic.
Bash scripts should not be used for their logic. Think of them as a way to document a series of commands that you do not want to manually type. That can be including long argument names, generating includes or classpaths, or generating log file names based on the current time.
A good rule of thumb is if your script involves any if-statement other than parameter validation or basic on-success-of-x-do-y, it probably should be in a real language.
You might want to be in the dash POSIX shell for raw speed and portability.
The defaults of bash make more sense for terminal use than scripting.
Powershell is a bit further towards the scripting end of the spectrum- part of why it annoys people coming from bash.
Compiled languages would be at the scripting end.
#!/bin/bash
set -e
grep nomatchforthis
echo "Won't get here!"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.