Search this site





bourne shell goodness.

So until now I never knew how to pipe things into my shell scripts - I'm smacking myself on the head now for not having figured this out sooner.

In unix, the standard file descriptors stdin, stdout, and stderr are numbered 0, 1, and 2, respectively. I've always done descriptor redirection, such as:

grep ^Poop myfile.txt 2>&1 > /dev/null

This will redirect stderr (fd #2) to stdout (fd #1). stdout is then redirected to /dev/null.

What I never bothered trying was redirecting stdin. (fd #0). Let's take a look at this simple script:


echo "Printing output:"
sed -e 's/^./Output: &/' <&0 

Note the <&0 - this will use our stdin as the stin for sed.
Now, let's try running it:
ls | ./myscript

The output is just how I want:

whack(~/bin) [590] > /bin/ls | sh
Printing output:
Output: Mrandom
Output: fix
Output: hostlist
Output: intarweb
Output: logtail
Output: malbums
Output: mclear
Output: minfo