ALL NOTES

pipelines · SEV-4

Pipeline step aborts with exit 2 and no error message

scroll to render

How to confirm it

  • Reproduce it in isolation

    set -eo pipefail
    COUNT=$(grep -c 'nothing-matches-this' /etc/hosts)
    echo "never printed: $COUNT"

    grep exits 1 when it matches nothing. Under set -e inside a command substitution, that kills the script before the echo.

  • Find every unguarded grep

    rg -n '\$\(.*grep' --glob '*.sh' | grep -v '|| true' | head -20

    Any command substitution containing grep without a guard is the same bug waiting for an input that matches nothing.

  • The fix

    COUNT=$(grep -c 'pattern' file || true)
    # or, when zero matches is meaningful:
    if grep -q 'pattern' file; then ...; fi

    A search finding nothing is not an error. Say so explicitly rather than letting the shell decide.

  • See where it died

    bash -x script.sh 2>&1 | tail -20

    Trace mode prints the last command executed before the abort, which is the one the exit code came from.

Read the source