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"

1 Answer 1

2

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.