Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.

set -e / errexit in shell

Table of Contents
Problem
Solution in Shell
Solution Oil
Style Guide

Problem

Solution in Shell

set +o errexit

my-complex-func
status=$?

other-func
status=$?

set -o errexit

Solution Oil

shopt -u errexit {
  var status = 0 

  my-complex-func
  setvar status = $?

  other-func
  setvar status = $?
}

Style Guide

No:

if myfunc ...             # internal exit codes would be thrown away

if ls | wc -l ;           # first exit code would be thrown away

Yes:

if external-command ...   # e.g. grep
if builtin ...            # e.g. test
if $0 myfunc ...          # $0 pattern

The $0 myfunc pattern wraps the function in an external command.


Generated on Sun Jan 19 22:59:37 PST 2020