1

To output both stdout and stderr from a script to the console and to a log file, I’ve found a beautiful solution with tee:

exec &> >(tee log.file)

The problem is, that sometimes tee seems to be eating newlines. For example, I have a trap on exit that prints a newline and only then exits.

trap "echo && exit 55" EXIT HUP INT QUIT KILL

It makes the shell print the prompt from the new line instead of wherever user might have pressed ^C while the script was running. And it doesn’t work, when there’s such a call to exec.

2
  • 1
    Bit confused. Could you explain how prompt getting a newline is related to tee eating newline?
    – Vikyboss
    Feb 25, 2016 at 18:44
  • @Vikyboss that’s what I would like someone to explain to me…
    – tijagi
    Feb 26, 2016 at 13:03

2 Answers 2

1

This method redirects stderr to stdout then pipes to tee. I think it's a little more readable.

exec 2>&1 | tee log.file

I've tested this on my machine and haven't encountered the "newline eating" problem.

1
  • Does this actually write the stdout of the script to the log.file? If so, may I know your version of Bash? Thanks.
    – Vikyboss
    Mar 16, 2016 at 3:30
1

tee is most likely being killed by the signal before receiving the newline.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .