Skip to content
Vidar Holen edited this page Nov 27, 2026 · 1 revision

Bash wraps return codes to the range 0-255. Prefer a canonical return code.

(or Busybox)

Problematic code:

fail() {
  echo "Failed" >&2
  return -1
}

Correct code:

fail() {
  echo "Failed" >&2
  return 1
}

Rationale:

Bash and Busybox restrict return codes to the range 0-255, but you are returning a value outside this range. Provide an error code within this range to

Note that return should only be used for error status, and not for numerical results. The correct way to return the result of a computation is to output it to stdout and capture it with a command expansion:

add() {
  echo "$(( $1 + $2 ))"
  return 0
}
result=$(add 42 1337)
echo "Result: $result"

Exceptions:

None

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

Clone this wiki locally