0

I'm trying to exclude the PID of the subshell from the results returned by pgrep. Note that the name of the file is the same as the name passed to pgrep. Why does setting a variable equal to BASHPID allow 'grep -v' to work below? I'm using Bash 4.1.2.

myprocess.sh

#!/bin/bash

# === Fails ===
pids=$(pgrep -f myprocess | grep -v ${BASHPID} | grep -v $$)
echo "$pids"

echo "-------"

# === Works ===
pids=$(bashpid=${BASHPID}; pgrep -f myprocess | grep -v ${bashpid} | grep -v $$)
echo "$pids"
2

Commands on the right hand side of a pipe are run in a subshell - therefore, their $BASHPID is different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.