$ recombobulate _
home / tips / use-claude-code-exit-codes-to-handle-failures-in-shell-scripts
129

Use Claude Code Exit Codes to Handle Failures in Shell Scripts

recombobulate @recombobulate · Mar 26, 2026 · Workflows
use-claude-code-exit-codes-to-handle-failures-in-shell-scripts

Claude Code behaves like a proper UNIX tool: it exits with code 0 on success and a non-zero code when something goes wrong. This means you can integrate it into shell scripts using the same patterns you use for any other command.

# Only commit if Claude's review passes
claude -p "Review src/auth.ts for security issues" \
  && git commit -am "feat: add auth module"

# Try a complex task, fall back to a simpler one if it fails
claude -p --max-turns 20 "Full migration with tests" \
  || claude -p --max-turns 5 "Apply migration only, skip tests"

# Check exit code explicitly
claude -p "Run the test suite and report any failures"
if [ $? -ne 0 ]; then
  echo "Claude Code encountered an error — check the output above"
  exit 1
fi

Non-zero exits happen when:

  • Claude hits the --max-turns limit before completing the task
  • A tool call fails and Claude cannot recover
  • The prompt is refused or a permission is denied mid-run

In CI pipelines, Claude Code steps fail the job automatically on a non-zero exit — exactly the same as any other command:

# GitHub Actions — job fails if Claude exits non-zero
- name: Security review
  run: claude -p "Audit this diff for vulnerabilities" --max-turns 10

Pair exit codes with --max-turns and --max-budget-usd to make every automated run predictable and safely bounded. A turn-limit exit is a signal to investigate — something took longer than expected.

Claude Code plays by UNIX rules — build on the exit code and your scripts become as reliable as any other tool in the pipeline.

~/recombobulate $ tip --comments --count=0

Log in to leave a comment.

~/recombobulate $ tip --related --limit=3
0
Run Claude Code in GitHub Actions to Automatically Review Every Pull Request

Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.

recombobulate @recombobulate · 1 month ago
0
Ask Claude to Build a Deployment Checklist from Your Actual Infrastructure

Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.

recombobulate @recombobulate · 1 month ago
0
Ask Claude to Generate a README from Your Actual Codebase — Not a Template

Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.

recombobulate @recombobulate · 1 month ago