Skip to main contentClaim $5 in free credit — one-time, per account. Claim $5 free
Krova CloudKrova Cloud
Developer Tools & CLI

Bash pipefail and tee: Keep Command Failures Visible

A complete log can hide a failed command. Use Bash pipefail and immediate PIPESTATUS capture to distinguish command and logger failures, then make the caller stop.

DM
Dhruv Malaviya5 min read
Share
Developer Tools & CLI cover with a Bash pipefail and tee headline, failure-checking subtitle, and a frosted-glass terminal connected to a log document with turquoise lighting.

Imagine you’re watching a deployment over SSH. You add tee so the output stays on screen and lands in a log. The command fails, but the surrounding job moves on. The log looks complete because the logger did its job. That is a useful record of failure, not evidence that the deployment succeeded.

In Bash, enable pipefail before the pipeline, inspect the result, and make the caller stop on failure. When you need to distinguish the command from the logger, copy PIPESTATUS immediately. Decide whether a logging failure should fail the job too. This guide uses that stricter policy.

A logger can succeed while the work fails

GNU’s Bash pipeline reference explains the default: a pipeline takes the last command’s exit status. With pipefail, it returns the rightmost nonzero status, or zero when every command succeeds. Changing that result does not, by itself, tell the rest of your script what to do.

set +o pipefail
bash -c 'printf "fixture output\n"; exit 7' | tee demo.log
printf 'pipeline exit=%s\n' "$?"

Run this only in a disposable directory: it creates or replaces demo.log. During preparation, this synthetic example printed its output and then pipeline exit=0 with Bash 5.2.37 and GNU Coreutils tee 9.7. The producer deliberately exited 7. No deployment was involved.

The GNU tee manual describes copying input to standard output and files, and reporting output errors through a failure status. A successful copy says nothing about the business meaning of the bytes. An error message can be copied just as successfully as a success message.

Preserve both statuses before printing a diagnosis

Save the following as run-with-log.sh in a controlled test directory, alongside an executable deploy.sh. Invoke it with bash run-with-log.sh. It uses Bash arrays and is not a portable /bin/sh script. Replace the producer only after you understand what its exit codes mean.

run-with-log.sh
#!/usr/bin/env bash
set -o pipefail

if ./deploy.sh 2>&1 | tee -- deploy.log; then
  printf 'Command and logger both returned zero.\n'
else
  result=( "${PIPESTATUS[@]}" )
  printf 'deploy exit=%s; tee exit=%s\n' \
    "${result[0]}" "${result[1]}" >&2
  if (( result[0] != 0 )); then
    exit "${result[0]}"
  fi
  exit "${result[1]}"
fi

The first command in the failure branch copies the array. Bash documents PIPESTATUS as the statuses of the most recent foreground pipeline, in command order. Later commands can replace it. Printing a helpful message before copying the array can therefore destroy the observation you wanted to report.

Here, index 0 belongs to deploy.sh and index 1 to tee. The wrapper deliberately returns the producer’s nonzero status first; if the producer succeeded but logging failed, it returns the logger’s status. That precedence is a policy choice. Keeping both values in the diagnostic avoids pretending one number describes every failure.

For two simultaneous failures, the returned value can differ from the pipeline’s aggregate status: pipefail chooses the rightmost failure, while this wrapper prioritizes the producer. Neither number identifies a root cause. A logger error may even affect the producer’s output path, so investigate the recorded pair.

The explicit if and exit paths keep the decision visible. Bash’s set reference documents important exceptions to errexit (set -e), including conditional contexts. Adding set -e everywhere is not a substitute for deciding which outcome the caller must honor.

Exercise the failure paths, including the log

The wrapper was checked locally with a synthetic producer that printed one line and returned either 0 or 7. To force a logger error without filling a disk, the fixture made deploy.log a directory. Each case used a separate temporary directory. These checks observed shell behavior on the versions above, not a production rollout.

Producer exitLog destinationObserved wrapper exitWhat the wrapper reported
0Writable file0Both returned zero
7Writable file7Producer 7, logger 0
0Directory instead of file1Producer 0, logger 1
7Directory instead of file7Producer 7, logger 1

The last row is worth keeping in your own rehearsal. Testing only a producer failure leaves the logging policy untested. Testing only the happy path leaves both decisions untested. Use harmless fixtures before replacing them with a command that changes data.

Then inspect the next layer. If another script invokes the wrapper and ignores its nonzero exit, the workflow can still continue. Put the wrapper’s result in that caller’s explicit success/failure branch and verify that the next stage is skipped. If the caller pipes the wrapper through another logger, audit that outer pipeline too.

Exit zero still needs an application check

This wrapper can preserve a failure that the producer reports. It cannot repair a deploy.sh that prints an error and exits zero, or prove that the new service is ready. Review the producer’s own error handling and keep readiness as a separate decision. Krova’s curl status-check guide shows why a completed HTTP transfer is not enough for that second check.

Logging also creates an operational artifact. This example combines standard error with standard output, so review what the command prints before keeping or sharing the file. Choose access permissions, a destination and a retention policy suitable for that output. Concurrent runs should have separate log paths; this small wrapper does not manage log rotation or redact secrets.

On a Krova Cube, you control the Linux guest and the scripts running inside it. Use a disposable working directory to check the actual Bash and tee versions you deploy. Root access does not make a shell pipeline’s success policy automatic.

Pick one command you currently pipe into a logger. Force a harmless producer failure, then a harmless log-path failure. Record both exit values and confirm what the caller does next. A useful green step is one whose success condition you can explain.

Rehearse your shell workflow on Cube

Explore a self-managed Linux environment for checking script exit behavior and application readiness with disposable fixtures.

Creation context: This AI-assisted guide uses linked primary documentation and a hypothetical deployment story. Only the local synthetic checks described above were performed; no customer deployment, production workload or performance benchmark was tested. The cover is a generated conceptual illustration.

For AI agents:llms.txtsitemap

Related posts