Thursday, May 10, 2012

Redirecting stderr and stdout in Bash

I can never remember the proper syntax for redirecting Stdout and Stderr so here it is.

Redirect Stdout to a file.

cmd > file.log

Redirect Stderr to a file.

cmd 2> file.log

Redirect both Stderr and Stdout to a file.

cmd &> file.log
cmd > file.log 2>&1


Stderr to stdout

cmd 2>&1 

Tying it together

Pipe only Stdout to a program.

cmd | less

Pipe only Stderr to a program.

cmd 2>&1 > /dev.null | less
cmd 3>&1 1>&2 2>&3 | less

Pipe both Stderr and Stdout to a program.

cmd 2>&1 | less

No comments: