The tee module executes a command and writes its output to both stdout and a specified file. Unlike traditional pipes, it preserves the original command's exit code and environment modifications.
When to Activate
- When logging build, test, or deployment outputs for CI/CD pipelines.
- When capturing both stdout and stderr while needing to react to command failures.
- When a command modifies environment variables that need to be maintained.
Core Principles & Rules
- Exit Code Integrity: Crucial for scripts where the following logic depends on the success of the logged command.
- Command Separation: Use the
--separator to distinguish between the output file and the command to run. - Append Mode: Support for
-ato append to the log file instead of overwriting.
Patterns & Examples
Log Build Output
# Capture build logs while ensuring the script stops on failure
x tee build.log -- make build
Capture Combined Output
# Log both stdout and stderr to a file
x tee /tmp/task.log -- eval 'npm run test 2>&1'
Logging Deployment
# Deploy and keep logs, allowing the exit code to be checked later
x tee deploy.log -- ./deploy.sh
Checklist
- Confirm if both stdout and stderr should be captured.
- Ensure the log file path is writable.
- Verify if append mode (
-a) is required.