Where to start First steps track · 15 stages
Linux Labs

Exit codes: $?, && and ||

cheat sheet · Ubuntu 24.04 · bash 5.2 https://lab.dimsanych.ru/en/wiki/exit-codes/

Cheat sheet

  • ls /nope; echo $? 2 — code of the last command
  • grep -q root /etc/passwd && echo found second runs after code 0
  • mkdir /srv/app || echo "failed" >&2 second runs after non-zero
  • if grep -q '^backup:' /etc/passwd; then echo found; fi
  • rc=$? save the code before it's lost
  • exit 1 leave a script with code 1
  • false | true; echo "${PIPESTATUS[@]}" 1 0 — code of every stage
  • set -o pipefail a failed stage fails it all
Loading…