We have three relevant streams when dealing with passing data around on the command line. STDIN (0), STDOUT (1) and STDERR (2)
echo “hello” will return “hello” to STDOUT
echo “hello” | sed s/llo/y/g
Returns: ‘hey’
echo “hello” will print “hello” to STDOUT which we pipe to sed’s STDIN. The shell will fork both processes, echo and sed, and create a pipe between one’s STDOUT to the other’s STDIN. A ‘broken pipe’ will occur when one terminates unexpectedly.
strace echo “hello” will print the system calls that the command makes. Lets say I just want to print out open() calls.
strace echo “hello” | grep open does not work. It seems that the grep is ignored.
This is because strace sends it’s output to STDERR and not STDOUT. In this case we must redirect STDERR to STDOUT so grep can pick it up on it’s STDIN.
strace echo “hello” 2>&1 | grep open will work successfully.
What if we want to redirect STDOUT and STDERR to a file? We simply redirect STDOUT to a file and then redirect STDERR to STDOUT.
strace echo “hello” >/tmp/strace.output 2>&1
A nonstandard method of achieving the same by redirecting everything in one go is strace echo “hello” &>/tmp/strace.output however this is not guaranteed to work across all implementations.
* Post edited thanks to observations from Adam Bolte (16/11/09)
Tags: echo, grep, pipe, redirect, sed, stderr, stdin, stdout