Sample Header Ad - 728x90

$(pgrep -f) behaviour changes within a script depending on the shebang

2 votes
1 answer
241 views
When I run the following script: #!/bin/bash $(pgrep -u ubuntu -f ${1} > /dev/null) echo "CURRENT_STATUS="${?} with: ./my_script.sh top when top is not running, it returns: CURRENT_STATUS=0 which is odd, as I would expect a 1 exit status from pgrep. When I remove the shebang from the script it works as expected. Could someone help me understand what's going on here? This is on an Ubuntu 22.4.1 system. Also note that the $(...) doesn't change the results. I have noticed that pgrep seems to be matching against itself and that's why it gives a 0 result no matter what process name I feed it.
Asked by ozpac (33 rep)
Jan 23, 2023, 05:35 PM
Last activity: Jan 23, 2023, 07:30 PM