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"