# set -e killed my scripts when nothing was found

`set -euo pipefail` at the top of every script is good advice. it also means any command that [exits non-zero for a perfectly normal reason](https://mywiki.wooledge.org/BashFAQ/105) takes the whole script down. it got me twice in the lab, and nearly a third time.

## grep exits 1 when it finds nothing

my deploy script diffed cron jobs between the repo and the server:

```bash
LOCAL_JOBS=$(cron_jobs < "$CRON_ROOT")
```

`cron_jobs` strips comments and blank lines (`grep -vE '^[[:space:]]*#' | sed ...`). one server's crontab was all comments, so grep matched nothing, exited `1`, and with `pipefail` the whole `$( )` failed and `set -e` killed the deploy halfway. same thing on a server with no crontab at all, because `crontab -l` exits non-zero when there's no table.

easy to see:

```bash
$ printf '# just comments\n' > ct.txt
$ bash -c 'set -euo pipefail; jobs=$(grep -v "^#" ct.txt); echo reached'
$ echo $?
1
```

`reached` never prints.

## the backup that wrote nothing

the backup script on one of my servers copied a website folder I'd since retired. `cp` on a missing path exits `1`, strict mode stopped the script before it compressed anything, and the weekly backup produced nothing. nothing told me.

another server's backup script had the same trap waiting: a `sqlite3` line pointing at a database path that had been deleted. it never ran against it, the box was off, but under strict mode it would have aborted the same way.

## the fix, and what it costs

for "no match is fine", `|| true`:

```bash
LOCAL_JOBS=$(cron_jobs < "$CRON_ROOT" || true)
```

for a path that might be gone, check it first: `[[ -d ~/docker/website ]] && cp -r ...`.

`|| true` has a catch with [`pipefail`](https://man7.org/linux/man-pages/man1/bash.1.html#SHELL_GRAMMAR) on. it covers the whole pipeline, not just grep. the remote side of the diff was:

```bash
REMOTE_JOBS=$(ssh "$REMOTE" "sudo -S crontab -l -u root 2>/dev/null" <<< "${N_SERVER_SUDO:-}" | cron_jobs || true)
```

if ssh fails, that's swallowed too, `REMOTE_JOBS` is empty, and the diff says the server has no jobs:

```bash
$ bash -c 'set -euo pipefail; R=$(false | grep -v "^#" || true); echo "continued: [$R]"'
continued: []
```

in a script where an empty list means "remove everything", that `|| true` is a real bug.

the narrower version only forgives grep's `1`:

```bash
cron_jobs() { grep -vE '^[[:space:]]*#' || [[ $? -eq 1 ]]; }
```

tested: no match passes, a failed command upstream still stops the script.

---

see also: [process tricks](/articles/bash-process-tricks), [every problem](/articles/every-problem)
