The Plumber’s Analogy

So I was working on a bash script, trying to remember how output redirection is ordered (I finally figured it out by trial and error) when I came up with an elegant analogy/mnemonic.  Here’s the command (redirection italicized):

kill $(pidof -x -o %PPID thisscript) >>/dev/null 2>&1 &&
 echo "killed older instance(s) of me" >&2
demo of "plumbing" analogy
demo of “plumbing” analogy

Imagine a grid two columns wide by several high.  The left is stderr (WLG) and the right is stdout.  bash reads from left to right and builds redirection apparatus (i.e plumbing) from bottom to top.  Clear as mud?

... >>/dev/null 2>&1 ...

is really the image on the right
I always had trouble with this and I hope this helps.

*NIX is surprisingly plumbing-like — that is, it’s data oriented as well as task oriented.

~ by Kevin on December 29th, 2008.

Leave a Reply

You must be logged in to post a comment.