Why doesn't “echo” show up in “ps”?

2019-07-22 03:53发布

问题:

I am having great difficulty in understanding what shows up on ps command. To test my understanding I created below dummy script

 #!/bin/bash
 for i in {1..100000}
 do
  date -u
  date -u
  date -u
 done

while running this script I opened a new terminal and executed repeatedly

ps -eaf | grep date | grep -v grep

and I was able to date process in the output.

I later changed dummy script by replacing date -u with echo "what is going on"

     #!/bin/bash
     for i in {1..100000}
     do
      echo "What is going on"
      echo "What is going on"
      echo "What is going on"
     done

while running the updated dummy script, I opened a new terminal and executed repeatedly

ps -eaf | grep echo | grep -v grep

and echo was never shown in output. Why is this? I suspect the reason is the script being a bash script, may be it is using builtin echo therefore it was not displayed in ps output. Am I correct? What am I missing here?

回答1:

echo is a builtin in bash:

$ type echo
echo is a shell builtin

That means that a new process is not created when echo is run. All the work is done by the bash process instead, which is way more efficient.

You can run the non-builtin echo explicitly:

command echo "What is going on"

This forks and execs /bin/echo instead, letting it show up in ps.



标签: linux bash unix ps