1

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?

cacamailg
  • 111
  • 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 05 '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 05 '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 05 '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 05 '13 at 13:16

2 Answers2

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
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
  • That is why I wanted a log file though :). – cacamailg Jul 05 '13 at 17:28