Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am running valgrind to shake out bugs in my program. Valgrind's memcheck prints to stderr. What I want to do is a modification to this:

valgrind --tool=memcheck ./a.out >output 2>grindout

But, I also want valgrind output to display on screen. So program's output all goes to file. Valgrind output is placed in grindout and displayed to screen. How do I do so?


share|improve this question
valgrind --tool=memcheck appname 2>&1 | tee valgrind.log from – RobotHumans Sep 14 '12 at 13:26
Are you still looking for an answer? Did you find a solution? How are the existing answers? – Jjed Sep 30 '12 at 18:57

You're looking for the tee command. Unfortunately there's some trickery regarding redirecting stderr and stdout. The following should work:

valgrind --tool=memcheck ./a.out 3>&1 2>&3 >output | tee grindout

This redirects standard output to output, moves standard error to a new file descriptor 3, which points to stdout. tee takes this stdout-ified stderr and prints it both to your screen and to gridnout.

share|improve this answer
Thanks, but I want stderr to go to screen and to file. But stdout to go to file and NOT to screen. – Badministrator Sep 14 '12 at 13:17
@Badministrator I've updated my answer to get that behavior. It's unexpectedly complicated (see this Stackoverflow question). – Jjed Sep 14 '12 at 13:40

Use tee for this, but you need to play some tricks with the stdout / stderr.

valgrind --tool=memcheck ./a.out 2>&1 > output | tee grindout

Explanation: 2>&1 sends stderr to stdout, and the regular stdout gets send to file output. The tee program passes on the stdin, but it also saves it to file grindout.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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