I know how to redirect stderr
to a file or to the stdout
when I issue a command: command 2 > log
or command 2 > &1
or even command 2 > &1 > /dev/null
. However, I want to redirect all the stderr
to a file since I open a bash console and thus all error messages of executed commands in the console would be redirected to that file. Is that possible? If so, what is the command to put in the .bashrc
?
Asked
Active
Viewed 2,391 times
1

cacamailg
- 111
2 Answers
4
If you want to log the errors but also see them:
exec 2> >(tee -a error.log)
Hmm, just tested that, and it logged the whole session. Anyone know why?

glenn jackman
- 85,964
-
-
Well I don't want to see the messages in the terminal, just log them. – cacamailg Jul 05 '13 at 12:55
-
-
Heh awesome. Here is some magic to do the opposite and duplicate stdout on stderr
exec 1> >(tee >(cat 1>&2))
– KCD Jul 24 '18 at 09:01
3
You can use
exec 2> log
The documentation states:
If COMMAND is not specified, any redirections take effect in the current shell.

Hauke Laging
- 90,279

choroba
- 47,233
-
This doesn't work for me! I am using bash 4.2.37(1), on Debian stable. It says
bash: exec: 2: not found
. – cacamailg Jul 05 '13 at 12:52 -
@cacamailg The space before the
>
was illegal (with respect to redirection). – Hauke Laging Jul 05 '13 at 13:20 -
@HaukeLaging Probably it is my bash version. But thsi solution seem to freeze my terminal. Probably what would work (which I know it is not a good thing to do) is to
ln -s $HOME/error.log /dev/stderr
. – cacamailg Jul 05 '13 at 13:23 -
@cacamailg: if you redirect stderr to a file, then you won't see any bash prompts nor will you see the what you type echoed to the terminal. It's not that the terminal is frozen; you can type, and you will see the output (stdout only) of every command. It's just that you will be typing blind. Why are you trying to do this? – rici Jul 05 '13 at 15:44
-
Ah! I see. Well I wanted that because some programs started from the terminal send a lot of warnings outside, which makes it full of not-useful messages making it hard to use. As such, if I had a way to do it for all commands instead of doing every time I issue a new command. – cacamailg Jul 05 '13 at 16:28
-
@cacamailg: you should be able to turn off excessive warnings, but you'd have to do that on a command by command basis. Aside from the terminal echoing issue, stderr is supposed to be used for error messages, and if you suppress error messages for all commands, you're going to often find yourself wondering why things don't work as you expect. – rici Jul 05 '13 at 16:31
-
.bashrc
so that when I open a terminal, all error/warning messages that would be displayed in the screen are now written to a file (which I can see later), somehow like the.xsession-errors
. – cacamailg Jul 05 '13 at 13:02exec > >(tee "$HOME/somefile.log") 2>&1
, doesn't seem to do what I want; that is, if I runvim
I get the messageVim: Warning: Output is not to a terminal
. – cacamailg Jul 05 '13 at 13:14