#!/bin/sh
set -euo pipefail
thanks, @b0rk - I've been writing shell for 25+ years and wish I'd been doing this 25+ years ago.
#!/bin/sh set -euo pipefail thanks, @b0rk - I've been writing shell for 25+ years and wish I'd been doing this 25+ years ago. 10 comments
@gremlins @darkuncle @b0rk orrr, if you are using an actual posix shell… (expression) || (echo 'error: thingy happened wrong'; break) will solve most of these 'issues'. @darkuncle @b0rk habitually setting pipefail will drive you crazy next time you use grep -q. @darkuncle @b0rk Hrm. So, 1) checking $? is shell-agnostic and generally more appropriate, 2) never rm recursively if you can help it, and check your variables prior to throwing them into something potentially destructive, and 3) how did that grep succeed? In general, shell programming isn't any different from anything else - beware of assumptions.
@darkuncle @b0rk beware: `-o pipefail` is not defined in POSIX spec, so `/bin/sh` may not recognize it (`/bin/bash` would) See ShellCheck Wiki for a POSIXly correct way of achieving a similar effect: https://www.shellcheck.net/wiki/SC3040 @kytta set -o pipefail has been added in POSIX.1-2024. https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/V3_chap02.html#tag_19_26 One I find really helpful (but missing here) is 'set -x', that prints every line during execution. @darkuncle @b0rk |
@darkuncle @b0rk #altText4u