Yohan Obadia
Yohan Obadia

Reputation: 2672

Difference in count with ps | wc

When I print the processes, I get:

$ ps --no-headers
12961 pts/0    00:00:00 bash
16676 pts/0    00:00:00 ps

So, their are only two processes. However, when I do:

$ ps --no-headers | wc -l
3

Any idea why ?

Upvotes: 3

Views: 673

Answers (1)

Hannu
Hannu

Reputation: 12205

Your wc process is the third one. So the count is correct in both cases and everything works as expected. The piped process starts immediately upon hitting enter, not after ps has finished and spit out its output.

If you do not want this to happen, you can use a temporary file.

ps --no-headers > /tmp/foo
wc -l /tmp/foo

would again produce two.

Upvotes: 4

Related Questions