Stop losing your place when script output goes sideways
Shell Scripting / Bash Tricks
Stop losing your place when script output goes sideways
🧩 The Challenge
You know that feeling when you run a backup script and the terminal output just turns into a million lines of scrolling text that you can’t possibly read? It’s a nightmare trying to scroll up to see where the actual errors started.
💡 The Fix
Use the exec command to redirect your script’s output to a file and the screen at the same time. You keep your sanity by having a persistent log while the live feed keeps you aware of what’s happening.
exec > >(tee -a script.log) 2>&1
⚙️ Why It Works
By redirecting stdout and stderr to the process substitution of tee, every single byte your script spits out is cloned into a file for later inspection and your terminal window for real-time monitoring. It effectively pipes the entire session output without you having to manually tack >> logfile onto every single command.
🚀 Pro-Tip: Add date stamps to your logs by swapping tee for awk ‘{print strftime(“[%Y-%m-%d %H:%M:%S]”), $0; fflush()}’ or just use ts from the moreutils package.
Linux Tips & Tricks | © ngelinux.com | 11/16/2026
