tee Command Reference for CI Scripts
tee copies stdin to one or more files and also passes it through to stdout.
tee lets a CI step both stream output to the console and capture it to a log file for later upload. The catch is how it interacts with pipefail and exit status.
Common flags/usage
- tee FILE: write stdin to FILE and to stdout
- -a / --append: append instead of truncating
- tee FILE1 FILE2: write to several files at once
- -i: ignore interrupt signals
- sudo tee: the idiom for writing to a root-owned file via a pipe
Example
shell
./build.sh 2>&1 | tee "build-${GITHUB_RUN_ID}.log"
echo "deb https://repo.example.com stable main" \
| sudo tee /etc/apt/sources.list.d/example.listIn CI
cmd | tee log.txt makes the pipeline exit status that of tee (usually 0), so a failing cmd is masked unless set -o pipefail is on; with pipefail the real failure propagates. sudo tee is the standard way to write a root-owned file from a pipe, since redirection runs as the unprivileged shell.
Key takeaways
- Without pipefail, cmd | tee hides a failing cmd behind tee\u2019s exit 0.
- sudo tee writes root-owned files where shell redirection cannot.
- Use -a to append to a running log instead of truncating it.
Related guides
set -euo pipefail Reference for CI Scriptsset -euo pipefail makes bash CI scripts fail fast. Reference for -e exit-on-error, -u unset-var, and pipefail…
tail Command Reference for CI Scriptstail prints the last lines of input or follows a growing file in CI. Reference for -n, -f, and +N, plus the -…
printf Command Reference for CI Scriptsprintf formats output predictably in CI, unlike echo. Reference for format specifiers, %s, %b, and escapes, p…
head Command Reference for CI Scriptshead prints the first lines or bytes of input in CI. Reference for -n, -c, and negative counts, plus the SIGP…