Bash Exit Codes

Bash Exit Codes

Bash Exit Codes

In Bash scripting, exit codes (or return codes) indicate the success or failure of a script or command. They are essential for debugging, automation, and creating robust scripts.

What Are Exit Codes?

An exit code is a numeric value returned by a command or script when it finishes execution. By convention:

  • 0: Indicates success.
  • Non-zero: Indicates failure or an error.

The exit code of the last executed command is stored in the special variable $?.

How to Use Exit Codes

Check the Exit Code of a Command

#!/bin/bash ls /some/directory echo "Exit code: $?"

Output (if directory exists):

Exit code: 0

Output (if directory doesn't exist):

ls: cannot access '/some/directory': No such file or directory Exit code: 2

Setting Exit Codes in Scripts

Use the exit command to explicitly set an exit code in your script.

Example:

#!/bin/bash if [ "$1" == "success" ]; then echo "Success!" exit 0 else echo "Failure!" exit 1 fi

Execution:

bash script.sh success

Output:

Success!

Exit Code:

0

Common Exit Codes

Exit CodeMeaning
0Success
1General Error
2Misuse of shell builtins
126Command invoked cannot execute
127Command not found
128Invalid argument to exit
130Script terminated by Ctrl+C

Example: Using Exit Codes in Automation

Basic Script Example

#!/bin/bash ping -c 1 google.com if [ $? -eq 0 ]; then echo "Internet connection is active." exit 0 else echo "Internet connection is down." exit 1 fi

Execution:

bash check_connection.sh

Output (if connected):

Internet connection is active.

Output (if not connected):

ping: google.com: Name or service not known Internet connection is down.

Using Exit Codes with Conditional Statements

Example:

#!/bin/bash if command -v git &> /dev/null; then echo "Git is installed." exit 0 else echo "Git is not installed." exit 127 fi

Execution:

bash check_git.sh

Output (if Git is installed):

Git is installed.

Exit Code:

0

Best Practices for Exit Codes

  1. Always Set an Explicit Exit Code: Avoid relying on the default exit code to ensure clarity.

    exit 0
  2. Use Descriptive Exit Codes: Assign different codes for specific error conditions.

    if [ -z "$1" ]; then echo "No argument provided." exit 2 fi
  3. Handle Exit Codes in Scripts: Use set -e to automatically exit a script if any command fails.

    set -e
  4. Document Exit Codes: Clearly define the meaning of each exit code in your script.

    # Exit Codes: # 0 - Success # 1 - General error # 2 - Missing arguments

Debugging with Exit Codes

Trace Script Execution

Use set -x to debug scripts and see the execution flow.

set -x

Trap Errors

Use trap to handle errors and exit codes cleanly.

#!/bin/bash trap 'echo "An error occurred. Exiting with code $?"; exit $?' ERR # Example command ls /nonexistent

Conclusion

Understanding and using exit codes in Bash is vital for creating scripts that handle errors gracefully. By leveraging exit codes, you can write robust, maintainable, and automated solutions.

Let me know if you’d like further examples or advanced use cases!

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close