Linux Bash Exit Code 2 — Misuse of Shell Builtin
About Linux Bash Exit Code 2
Bash exit code 2 indicates incorrect usage of a shell builtin command, typically due to wrong arguments or syntax errors. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: Exit code 2 is defined by bash for misuse of shell builtins. Often appears when passing invalid options or arguments. Many GNU utilities also return 2 for usage errors. Different from exit code 1 (general error) in that it specifically indicates wrong usage. The command was found and executed but rejected the arguments. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Invalid command-line option or flag. Missing required argument for a command. Incorrect syntax in shell built-in (test, [, [[). Wrong number of arguments to a function. Unrecognized option passed to a GNU utility. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Read the command usage: command --help or man command. Check the exact syntax — some flags require = sign or spaces. Verify option flags match the installed version of the command. Use the long form of options for clarity: --verbose instead of -v. Check if the command is a shell builtin or external: type command_name. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
What is a shell builtin?
Commands built into bash itself (cd, echo, test, [, source, export) as opposed to external programs like ls or grep.
Overview
Bash exit code 2 indicates incorrect usage of a shell builtin command, typically due to wrong arguments or syntax errors.
Key Details
- Exit code 2 is defined by bash for misuse of shell builtins
- Often appears when passing invalid options or arguments
- Many GNU utilities also return 2 for usage errors
- Different from exit code 1 (general error) in that it specifically indicates wrong usage
- The command was found and executed but rejected the arguments
Common Causes
- Invalid command-line option or flag
- Missing required argument for a command
- Incorrect syntax in shell built-in (test, [, [[)
- Wrong number of arguments to a function
- Unrecognized option passed to a GNU utility
Steps
- 1Read the command usage: command --help or man command
- 2Check the exact syntax — some flags require = sign or spaces
- 3Verify option flags match the installed version of the command
- 4Use the long form of options for clarity: --verbose instead of -v
- 5Check if the command is a shell builtin or external: type command_name