Friday, April 6, 2007

Protecting Grep

So here's some real Arcana for you. This comes from the UNIX family of spells - some of the most-difficult-to-master magic out there.

Grep seems to need protection when it's used to process directory information, as in:
ls /some/path | grep -e [some pattern]
This code may work differently if "/some/path" is a full path to the folder rather than ".". Issuing
env -i ls /some/path | grep -e [some pattern]
appears to solve the problem.

Who knows why grep needs it. Took me quite a while to figure out the solution to the problem, too. Which has resulted in a new bit of core UNIX lore, for me: when in doubt, blow away your environment with env prior to issuing your command.

No comments: