in reply to Finding commands in Unix PATH

Zsh keeps a hash of all commands in PATH (updated when you run hash -r and/or twiddle PATH) in a parameter.

$ for i in ${(onk)commands}; [[ "$i" == *grep* ]] && print $i bzegrep bzfgrep bzgrep egrep fgrep git-grep grep grep-changelog msggrep pcregrep podgrep tcgrep xml_grep zegrep zfgrep zgrep zipgrep

Granted that's a zsh pattern not a regexp (however if you really want to you can compile in pcre support when you build zsh :). Functionalization left as an exercise for the reader.

Update: Ooop, note that that's run with setopt short_loops; without that you'd need the usual shell-y do ... done around the body.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Finding commands in Unix PATH
by jdporter (Paladin) on Jun 05, 2008 at 13:50 UTC
    perl -le "/grep/ and print for @ARGV" ${(onk)commands}
    whatever ${(onk)commands} is/does. I'm not familiar with the peculiarities zsh.

      Zsh has several flags that affect parameter expansion. The k flag says to expand a hash into a list of its keys (rather than the default which is to expand into a list of its values; or you index it like ${commands[ls]} and get the path to ls). The o flag says to sort the resulting expansion by name in ascending order (the on is actually wrong but works; I was confuzzling glob expansion qualifiers where it's oX where X specifies what to sort on (e.g. n for filename, or m for modification time)).

      So Perl's in a run for its money with regards to line-noisy-ness compared to terse zsh. :)

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.