v0.3.2

Documentation version

v0.2.4v0.3.2
en

Documentation language

en

Search documentation

ESC

Documentation search

What are you working on?

Find a guide, configuration option, action, or ESH topic.

Browse documentation
DocsControl flow and failures

Control flow and failures

ESH follows the shell convention of reporting command success through an exit status. Use that status to make decisions, clean up, and report useful diagnostics.

Choose the next step

Use if, case, loops, and functions to keep script decisions close to the work they control.

classify_archive() {
  case "$1" in
    *.tar.gz|*.tgz)
      echo "tarball"
      ;;
    *.zip)
      echo "zip"
      ;;
    *)
      echo "unsupported" >&2
      return 1
      ;;
  esac
}

for ARCHIVE in "$@"; do
  classify_archive "$ARCHIVE" || exit 1
done

&& continues only after success; || provides a recovery branch. set -e stops the current failure boundary when an unhandled command fails. Use it when continuing after a failed step would be misleading.

Preserve the reason for failure

A grouped block can provide one failure path for several operations. When a command fails, ESH exposes the command and line through telemetry variables.

{
  cargo fmt --check
  cargo test
  archive ./dist/checks.zip ./reports
} || {
  echo "Failed: $ESH_FAILED_CMD at line $ESH_FAILED_LINE" >&2
  exit 1
}

$? contains the status of the most recently completed command. trap runs a handler for events such as EXIT and ERR.

Clean up every path

Create temporary files or directories once, then register their cleanup immediately. The handler runs on both successful and failing paths.

TEMP="$(mktemp -d)"
trap "rm -rf '$TEMP'" EXIT

cp -r ./src "$TEMP/source"
echo "Prepared $TEMP/source"

For exact syntax and edge cases, including set -u, pipefail, redirections, and multi-level break, see the ESH Language Reference.

Next: run independent work in parallel.

Page source: book/esh/30.control-flow-and-failures.md