Skip to main content
concision
Source Link
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 

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 ps output (in a local variable 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_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i for 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.

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 ps output (in a local variable so it goes away when 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_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i for case-insensitive and with all of the arguments handed this function (in the case of no arguments, ^, which matches the start of any 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.

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 ps output (in a local variable so it is scoped to the function)
  • 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_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i (case-insensitive) and all of the options handed to this function (in the case of no pattern, use ^ to match the start of each line)

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.

"options" not "arguments", moved call-out to bottom, modern code blocks
Source Link
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. I briefly had it in my rc files, but bash complains about that syntax even despite a conditional that should prevent its evaluation.


From my rc files, I have a case-insensitive version that takes grep's argsoptions:

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 ps output (in a local variable so it goes away when 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_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i for case-insensitive and with all of the arguments handed this function (in the case of no arguments, ^, which matches the start of any 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.

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. I briefly had it in my rc files, but bash complains about that syntax even despite a conditional that should prevent its evaluation.


From my rc files, I have a case-insensitive version that takes grep's args:

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 ps output (in a local variable so it goes away when 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_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i for case-insensitive and with all of the arguments handed this function (in the case of no arguments, ^, which matches the start of any line to match everything)

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 ps output (in a local variable so it goes away when 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_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i for case-insensitive and with all of the arguments handed this function (in the case of no arguments, ^, which matches the start of any 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.

slightly better phrasing
Source Link
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. I briefly had it in my rc files, but bash complains about that syntax even despite a conditional that should prevent its evaluation.


From my rc files, I have a case-insensitive version that takes grep's args:

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 ps output (in a local variable so it goes away when the function completesreturns).
  • 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_OUTPUT$PS_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title.
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i for case-insensitive and with all of the arguments handed this function, or, in (in the case of no arguments, ^, (matchwhich matches the start of any line, which matches to match everything).

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. I briefly had it in my rc files, but bash complains about that syntax even despite a conditional that should prevent its evaluation.


From my rc files, I have a case-insensitive version that takes grep's args:

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 ps output (in a local variable so it goes away when the function completes).
  • 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_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title.
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i for case-insensitive and with all of the arguments handed this function, or, in the case of no arguments, ^, (match the start of any line, which matches everything).

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. I briefly had it in my rc files, but bash complains about that syntax even despite a conditional that should prevent its evaluation.


From my rc files, I have a case-insensitive version that takes grep's args:

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 ps output (in a local variable so it goes away when 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_OUTPUT and remove everything after the first line feed (regex equiv: s/\n.*$//msg). This prevents us from grepping the title
  • Display the ps output with everything except the first line (regex equiv: s/^.*\n//m) and grep its contents with -i for case-insensitive and with all of the arguments handed this function (in the case of no arguments, ^, which matches the start of any line to match everything)
zsh just started breaking with backticks (`\`command\``), moving to quoted parens (`"$(command)"`)
Source Link
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35
Loading
nixed zsh-only code, cleaner implementation, detailed code walk, regex equivalents to shell variable substitutions
Source Link
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35
Loading
bash complains about `=(command)` syntax, even in a conditional that should prevent its evaluation
Source Link
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35
Loading
found a better way to do it, simplified examples, improved explanations, removed suboptimal alternatives
Source Link
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35
Loading
Source Link
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35
Loading