Parse 7-Zip Exit Codes in Bash Scripts

Automating archive creation or extraction with 7-Zip requires robust error handling to ensure data integrity and pipeline stability. 7-Zip communicates execution results through standardized numeric exit codes, which indicate whether an operation succeeded, encountered locked files, ran out of memory, or failed completely. This guide explains what each 7-Zip exit code means, how to capture the return code immediately in Bash, and how to implement a structured pattern using case statements to handle warnings and fatal errors dynamically.

7-Zip Exit Code Reference

7-Zip (7z, 7za, or 7zr) returns specific integer values upon termination:

Exit Code Meaning Description
0 Success No errors occurred during the operation.
1 Warning Non-fatal error(s) occurred (e.g., files were locked or skipped).
2 Fatal Error A critical error occurred (e.g., corrupt archive, disk full).
7 Command Line Error Bad parameters, wrong flags, or invalid syntax.
8 Out of Memory Not enough memory to complete the operation.
255 User Stopped The process was aborted manually (e.g., Ctrl+C / SIGINT).

Capturing Exit Codes in Bash

In Bash, the special parameter $? holds the exit status of the most recently executed foreground command. Because $? is overwritten by every subsequent command—including echo or variable assignments—you must save it immediately after the 7-Zip execution:

7z a -tzip backup.zip /path/to/data/
EXIT_CODE=$?

Implementation: Comprehensive Error Handling

The most effective way to process 7-Zip exit codes in automation is a case statement. This allows you to handle partial successes (such as warnings where files were locked) differently from critical failures.

#!/usr/bin/env bash

ARCHIVE_NAME="archive.7z"
SOURCE_DIR="/var/log/app"

# Run 7-Zip command
7z a "$ARCHIVE_NAME" "$SOURCE_DIR" > /dev/null 2>&1
EXIT_CODE=$?

# Parse the return code
case $EXIT_CODE in
    0)
        echo "[SUCCESS] Archive created successfully."
        exit 0
        ;;
    1)
        echo "[WARNING] Archive created, but some files could not be processed (e.g., locked files)."
        # Decide whether to treat a warning as an error or allow the script to proceed
        exit 1
        ;;
    2)
        echo "[FATAL ERROR] 7-Zip encountered a fatal error." >&2
        exit 2
        ;;
    7)
        echo "[SYNTAX ERROR] Incorrect command-line arguments provided." >&2
        exit 7
        ;;
    8)
        echo "[RESOURCE ERROR] 7-Zip ran out of memory." >&2
        exit 8
        ;;
    255)
        echo "[ABORTED] The operation was terminated by the user." >&2
        exit 255
        ;;
    *)
        echo "[UNKNOWN ERROR] 7-Zip exited with unexpected code: $EXIT_CODE" >&2
        exit "$EXIT_CODE"
        ;;
esac

Handling Exit Codes in Scripts Using set -e

If your script utilizes set -e (exit immediately on non-zero exit status), non-zero return codes from 7-Zip will terminate your script before you can evaluate them. Prevent premature termination by using an || fallback or a conditional block:

set -e

# Prevent script exit by capturing the status directly
EXIT_CODE=0
7z x backup.zip -o/destination/ || EXIT_CODE=$?

if [ "$EXIT_CODE" -eq 0 ]; then
    echo "Extraction successful."
elif [ "$EXIT_CODE" -eq 1 ]; then
    echo "Extracted with non-fatal warnings."
else
    echo "Extraction failed with exit code $EXIT_CODE." >&2
    exit "$EXIT_CODE"
fi