Exit code

2 snippets across 2 stacks — Bash & Linux, Docker

SHBash & Linux

Exit Codes & Error Handling

SH · Bash Scripting
syntax
$?
set -e
set -o pipefail
command || handle_error
example
#!/usr/bin/env bash
set -euo pipefail

grep -q 'ready' status.txt || { echo 'Not ready'; exit 1; }

if ! deploy_app; then
  echo "Deploy failed with code $?"
  rollback
  exit 1
fi

Note $? holds the exit code of the last command (0 = success, non-zero = failure). set -e aborts the script on any non-zero exit. set -u treats unset variables as errors. set -o pipefail catches failures in piped commands. Always use all three in production scripts.

DKDocker

Wait for Container to Stop

DK · Containers
syntax
docker wait <container>
example
docker wait batch-job && echo 'Job finished'
output
0

Note Blocks until the container stops and then prints the exit code. Exit code 0 means success. Useful in scripts that need to wait for a task container to finish before proceeding.