Neat trick to capture STDERR into a text file for the next command but also exit with the preserved exit code after then. 1 2 3 4 5 6 7 8 9 10 11 12 gradle \ build \ 2> >(tee error.log >&2) EXIT_CODE=${PIPESTATUS[0]} if [[ -s "error.log" ]] && [[ ${EXIT_CODE} -ne 0 ]] then cat error.log | buildkite-agent annotate --style "error" fi exit $EXIT_CODE