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?

  • I think it is not a duplicate, since I want to run a command at every session that will log all error/warning messages to a file in that session. Let me make it clear, I want to write a line in .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 5 '13 at 13:02
  • @cacamailg It's a duplicate in that sense that one of the answers solves your problem (not the currently accepted one though). – Hauke Laging Jul 5 '13 at 13:07
  • @HaukeLaging not exactly. For example exec > >(tee "$HOME/somefile.log") 2>&1, doesn't seem to do what I want; that is, if I run vim I get the message Vim: Warning: Output is not to a terminal. – cacamailg Jul 5 '13 at 13:14
  • 1
    @cacamailg OK, I should have written: One of the answers uses the right idea to solve your problem. – Hauke Laging Jul 5 '13 at 13:16

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?

  • Works fine for me. (zsh 5.0.2 and bash 4.2.45) – Mel Boyce Jul 5 '13 at 12:29
  • Well I don't want to see the messages in the terminal, just log them. – cacamailg Jul 5 '13 at 12:55
  • @glennjackman: because bash uses stderr to echo what you type. – rici Jul 5 '13 at 16:02
  • 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 9:01

You can use

exec 2> log

The documentation states:

If COMMAND is not specified, any redirections take effect in the current shell.

  • 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 5 '13 at 12:52
  • @cacamailg The space before the > was illegal (with respect to redirection). – Hauke Laging Jul 5 '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 5 '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 5 '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 5 '13 at 16:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.