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 From my rc files, I have a case-insensitive version that takes grep's options:
psl() { local PS_OUTPUT="$(ps auxww)" echo "${PS_OUTPUT%%$'\n'*}" >&2 # title, in stderr to avoid pipes echo "${PS_OUTPUT#*$'\n'}" |grep -i "${@:-^}" } Code walk, one bullet per line of code:
- Capture the verbose
psoutput (in alocalvariable so it goes away whenis scoped to the function returns) - Display the first line (the title) in standard error so the results can be further filtered without affecting the title. The substitution says: take
$PS_OUTPUTand remove everything after the first line feed (regex equiv:s/\n.*$//msg). This prevents us from grepping the title - Display the
psoutput with everything except the first line (regex equiv:s/^.*\n//m) and grep its contents with-ifor case(case-insensitive) and with all of the argumentsoptions handed to this function (in the case of no argumentspattern, use^, which matches to match the start of anyeach line to match everything)
Call-out: @EmanuelBerg's grep fnord =(ps aux) answer is by far the most elegant, though it requires zsh. I briefly had it in my rc files, but bash complains about that syntax even despite a conditional that should prevent its evaluation.