1
 ps -e | grep -c '^'

Whatever it means I have 166

I got lost with the '^' and can't remember where I seen it

5

The -c (--count) option of grep prints the number of matches.

The ^ is a regular expression metacharacter representing the beginning of a line (and does not match any actual character).

Your command shows the count of all processes, as seen by ps.

Note that running the same command, removing the -c option from grep, should display the actual output your count is based on.

1
  • Thank you, that's what I thought but I questioned myself and though no way... – user610658 Nov 20 '16 at 23:54
3

The caret ^ is a regular expression line anchor - it restrict matches to occur only at the start of a line.

In the example you give, there is an empty pattern after the anchor, so it matches lines that start with anything - making it equivalent to simply using grep -c ''

Either way it returns a line count - equivalent to doing ps -e | wc -l

2
  • Grep -c don't output any results for me, and ps | wc -l outputs 4 compared to ps -e | grep -c '^' outputs 166 – user610658 Nov 21 '16 at 0:17
  • @hellomoto apologies I didn't mean to imply that ps and ps -e would give the same results - obviously they won't. I meant that ps <whatever options> | grep -c '' would be equivalent to ps <same options> | wc -l – steeldriver Nov 21 '16 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy