There are lots of questions regarding redirecting stderr
and stdout
for a single command or script. What I'd like is to redirect any stderr messages from my Bash session to a log file.
I'd like an interactive bash session where all stderr
is redirected to a file.
A horrible way to deal with your problem:
exec 3>&2
trap 'exec 2>>/path/to/your_file' DEBUG
PROMPT_COMMAND='exec 2>&3'
exec 3>&2
: we first copy fd 2 to a new fd (here fd 3)trap 'exec 2>/dev/null' DEBUG
: Before each command is executed (and if the shell option extdebug
is set, which is the default in interactive shells), the DEBUG
trap is executed: here we redirect stderr
to the file /path/to/your_file
(make sure you give an absolute path).PROMPT_COMMAND
variable: here we redirect fd2 to fd3 (and fd3 was a copy of fd2 when it pointed to the terminal). This is necessary to print the prompt.I wouldn't qualify this as a robust or nice method, yet it might do the job for your purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With