Skip to main content
2 of 8
found a better way to do it, simplified examples, improved explanations, removed suboptimal alternatives
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35

The simplest shell-agnostic way to do this would be to store it in a variable first:

PS_OUTPUT=`ps aux`; echo "$PS_OUTPUT" |grep fnord 

Call-out: @EmanuelBerg's grep fnord =(ps aux) answer is by far the most elegant, though it requires zsh.


From my rc files, I have a case-insensitive version that takes grep's args (GREP_COLOR=7 should invert your colors when you have $GREP_OPTIONS set to include something like --color=auto):

_psl_title() { ps auxww |GREP_COLOR=7 grep -m1 . >&2 # use stderr to avoid pipes } if type zstyle >/dev/null 2>&1; then # zsh psl() { _psl_title grep -i "${@:-^}" =(ps auxww) } else psl() { _psl_title local PS_OUTPUT=`ps auxww` echo "$PS_OUTPUT" |grep -i "${@:-^}" } fi 

"${@:-^}" yields a properly quoted list of the arguments, or, in the case of no arguments, "^" (match the start of any line, which matches everything).

Adam Katz
  • 4.2k
  • 1
  • 28
  • 35