2
BashERRORNotableExit CodeHIGH confidence

Misuse of shell builtins

What this means

Exit code 2 is returned by the shell itself to indicate the incorrect use of a shell builtin command. This is distinct from errors in external commands.

Why it happens
  1. 1Providing invalid arguments to a builtin command.
  2. 2Syntax errors related to a builtin.
How to reproduce

Using a builtin command like `cd` with an incorrect option.

trigger — this will error
trigger — this will error
#!/bin/bash
cd --invalid-option
echo "Exit: $?"

expected output

bash: cd: --invalid-option: invalid option
cd: usage: cd [-L|[-P [-e]] [-@]] [dir]
Exit: 2

Fix

Consult the documentation for the builtin

WHEN Encountering exit code 2

Consult the documentation for the builtin
help cd

Why this works

The `help` command provides the correct usage and options for shell builtins.

What not to do

Treat it like an external command failure

The problem lies within the script's syntax, not with an external program.

Sources

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Bash errors