r/commandline • u/speckz • Apr 15 '20
bash The first two statements of your BASH script should be…
https://ashishb.net/all/the-first-two-statements-of-your-bash-script-should-be/
88
Upvotes
r/commandline • u/speckz • Apr 15 '20
120
u/geirha Apr 15 '20
No, that's a terrible idea.
set -u
set -uis the least bad of the three, but is still a bit wonky. It will for instance cause a fatal error if you try to expand an unset string variable, but if it's an unset array, it just completely ignores that ... unless you're running bash 4.3 or older, in which case it's a fatal error ... but then trying to expand an empty declared array is also considered a fatal error ...-- set -u array=() for elem in "${array[@]}"; do printf '<%s>\n' "$elem" done printf 'still here\n' # Bash 4.3 | Bash 4.4+
# array[@]: unbound variable | still here
So depending on version it can either over-react or ignore an obviously unbound variable, and you have to add extra workarounds to account for those edge cases, just to catch some rare typoed variables that shellcheck and testing would reveal anyway.
set -e
In a similar fashion to set -u, set -e has changed behavior between bash versions, triggering fatal errors in one version, ignoring the same non-zero return value in another. In addition, whether a command returning non-zero causes a fatal error or not depends on the context it is run in. So in practice, you have to go over every command and decide how to handle their return value anyway. It's the only way to get reliable error handling. set -e adds nothing useful to the table.
See http://mywiki.wooledge.org/BashFAQ/105 for some examples of the weird cases you have to deal with.
set -o pipefail
You do NOT want to enable this option globally. The reason for this is that it's normal for commands in the left part of a pipeline to return non-zero without it being an error.
Consider the following case:
Looks pretty innocuous, doesn't it? You run a command and search its output for a word, and if the word is found it enters the
thenblock, if not, theelseblock. And in your testing it might look like its working "perfectly" too. You make cmd output a few lines, one of which contains word, and it prints "Found it". You do the same test without that line containing word, and it prints "Nope". As expected.Then your script enters production and it works as expected for a little while, but suddenly, when cmd's output has gotten a bit larger, it skips to the
elseblock even though the output clearly has the word in it.Why?
because commands typically buffer their output when it's not being sent to a terminal. When a C program does
printf("Something\n");and stdout is not a tty (it's a pipe in this example), the c library doesn't output it immediately. It appends it to a buffer of, say 4096 bytes. When the buffer gets full, it finally writes that full chunk to the pipe.Meanwhile grep has been idling, waiting for something to do, then suddenly a 4KiB chunk arrives and it starts looking for the word. Let's assume it finds it, so now grep is happy, it has done its job, it found at least one occurance of the word, so it happily exits with a return value of 0.
cmd doesn't know that though, it can't see past the pipe. It's still chugging along filling another buffer. When that buffer is finally full, it sends it off to the pipe again, but now the pipe is broken. grep closed the other end when it exited. What happens then is that the system send SIGPIPE to cmd to tell it it can't write any more to that pipe. By default, SIGPIPE causes the process to exit, and its return value will be 128 + 13 (the signal number of SIGPIPE).
Without pipefail, the return value of the pipeline would be 0 because the rightmost command returned 0, and it would jump to the
thenblock as expected.With pipefail, the return value of the pipeline is 141, thus it wrongly jumps to the
elseblock.pipefail is useful for pipelines where you know the command on the right will read the entire input. An example of such a case could be:
where you may want to know if either curl or tar failed, and tar must read all input to accomplish its task, so pipefail makes sense here.
In other words, use it with care, enable it before pipelines where it makes sense, disable it after. Do not enable it globally.
10
u/[deleted] Apr 15 '20
Not
set -e, that's for sure! Instead of dealing with all the rules around whenerrexitdoes not catch a non-zero exit code, just be explicit about the error handling.6
u/keepitsalty Apr 15 '20
I liked set -e until I realized I couldn't have boolean predicate functions without the script exiting. Now I just handle errors explicitly.
3
u/dotwaffle Apr 15 '20
Fun fact: It was first advertised to be:
(or bash if you prefer) but note that space between the
!and the/. The magic four characters#! /were what was originally looked for, though I'm told the space was [optional](https://en.wikipedia.org/wiki/Shebang_(Unix)#History).7
u/RotaryJihad Apr 15 '20
I'm a fan of `set -x` which prints out the commands being run by script
4
u/brwtx Apr 16 '20
Exit the script immediately after any failure? Sounds like someone who hasn't actually run critical infra with Bash scripts. No logging of the command output to syslog? No rolling back changes? No alerts via email and text message? Genius.
2
u/zebediah49 Apr 15 '20 edited Apr 15 '20
... Only if you never want to use
grepwithout invoking a pile of headaches.E: Since that only covers one of the two most annoying "you must always do this or your code is bad and should feel bad" set flags, I'll also add
This, of course, can be done was
[[ "$#" -gt 0 ]]... but that's a lot less obvious as to what it does IMO, and there are other places with there "Test unused variable" pattern is convenient.2
u/findmenowjeff Apr 15 '20
They really shouldn't be. Its a bad way to do error handling, fraught with pitfalls and edgecases, and usually lets the author get away with writing very poor code. I've written scripts for a variety of systems and never once seen a case where `-e`, `-u`, or pipefail was necessary when proper practices were used.
-6
u/ashfixit Apr 15 '20
bash is not POSIX compliant, unfortunate license, security vulns and all
first line should be to run the bourne shell instead
#!/bin/sh
... which supports some of the same assertion/debugging/compat behavior via set [-/+abCEefIimnpTuVvx] [-/+o longname] [-c string] [-- arg ...]
-5
u/seaQueue Apr 15 '20
Related, for anyone who regularly has to work with sh scripting as well:
0
u/[deleted] Apr 15 '20
[deleted] — view deleted comment