Here is a test:
$ bash -c "pgrep -f novalidname"
$ sh -c "pgrep -f novalidname"
11202
Why is pgrep
giving output when run from sh
? (As far as I can see, there are no processes on my computer that is named novalidname
)
Here is a test:
$ bash -c "pgrep -f novalidname"
$ sh -c "pgrep -f novalidname"
11202
Why is pgrep
giving output when run from sh
? (As far as I can see, there are no processes on my computer that is named novalidname
)
That's one thing (finding itself because of delay) see also:
Here it usually shows as well. (on Ubuntu does for me. (under bash)
The other thing is what is /bin/sh bound to?
On most Linux distros /bin/sh is a soft link to default shell which is usually actually bash, but can be any other shell.
The time difference that causes grep/pgrep to show itself may be introduced by finding a soft link location (hm, odd) or some other shell is bound to /bin/sh which executes slightly different than bash, thus causing the delay needed for process to show in pgrep.
Also, bash will firstly try to source ~/.bashrc and load its history, while /bin/sh will do what will do. In .bashrc can be pgrep defined as alias in another way which may also affect the difference.
To see where /bin/sh points to do:
Or just run sh to see what will show up. :D
The actual explanation:
Regardless of flags,
pgrep
never returns its own PID.If you execute
bash -c
with a simple command, then bash willexec
the command rather than creating a redundant subshell to execute it in. Consequently,bash -c "pgrep -f blah"
will replace thebash
process with apgrep
process. If thatpgrep
process is the only process whose command line includesblah
, thenpgrep
will not display any PIDs (as per 1).dash
does not perform the above optimization. (zsh
andksh
do.) So if on your system,sh
is implemented withdash
, thensh -c "pgrep -f blah"
will result in two processes being executed -- thesh
process and thepgrep
child -- both of which containblah
in their command lines.pgrep
will not report itself, but it will report its parent.It's probably a timing issue and
pgrep
finds itself, as you're issuing it with-f
andnovalidname
is present in the command line. Try with-l
to confirm.