Why doesn't "echo" show up in "ps"?
Asked Answered
K

1

2

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?

Karol answered 3/3, 2015 at 19:22 Comment(11)
As you surmise, echo isn't a process. It's a shell built-in command.Barrow
Original title was extremely vague -- and also something that couldn't be straightforwardly answered in a cross-platform way (POSIX's specification for ps output format leaves much to the individual implementation, unless one is assured of XSI compliance).Laminous
@Barrow Not necessarily. A lot of people think bash is the world's only shell. It is built into bash, zsh and a bunch of others. It is decidedly not built-in to sh, the Bourne shell. And there is indeed a /bin/echo executable in coreutils.Hereinafter
...though many POSIX sh implementations do build in echo.Laminous
BTW, what's the real-world problem you're trying to solve? Is this is for purposes of locking, or for ensuring a single process instance, ps is absolutely the wrong tool for the job (see flock).Laminous
It's not part of the spec, though. And a lot of Linuxen link /bin/bash to /bin/sh.Hereinafter
(also, insert here some grumbling about conflating POSIX sh with Bourne -- the latter being born about 20 years prior to the former).Laminous
@TrippKinetics yes that's true, and I know bash isn't the world's only shell. But the OP indicated bash in their tags, so I just naturally assumed that's what they are using.Barrow
Well, there's also weirdness like in my own default shell, I've bypassed some of the shell built-ins with aliases, but that's really outside the scope of the question...Hereinafter
@CharlesDuffy I need to echo password to curl -K, so that password can be read from stdin and not displayed in psKarol
If you'd asked whether a given practice for calling curl was secure, frankly, that would have been a more sensible question. Easier to answer in a cross-platform manner, too.Laminous
C
8

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.

Checkmate answered 3/3, 2015 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.