Here is a very strange question about using pgrep to search which shell processes are running the same script as the current one.
Here is the test script named test.sh
#!/bin/bash
full_res=`pgrep -a -l -f 'test\.sh'`
res=$(pgrep -a -l -f 'test\.sh' | cat)
echo "short result is $full_res"
echo "weird result is $res"
With output being
sh test.sh &
[1] 19992
➜ logs short result is 19992 sh test.sh
weird result is 19992 sh test.sh
19996 sh test.sh
[1] + 19992 done sh test.sh
I don't know where the 19996 sh test.sh
comes from, especially when using a pipe to cat. I believe it might be a bug to pgrep implementation.
Looking forward to some reasonable explanation
Thx,
Balin