How can I get the PID of the GNOME Terminal instance I'm using from within a Bash script?
I can run
xprop _NET_WM_PID and then manually click the terminal window, but I'd like to completely automate this.
To be safe though, the following will find the parent gnome-terminal process even if multiple bash shells are nested:
The following universal version will work for any shell, even if other grep instances are running. Deciphering it is left as an exercise for the reader ;)
pstree -p -a -s \ $(pstree -p -a | grep -B3 $RANDOM$RANDOM \ | grep -m1 `echo $SHELL |cut -d/ -f3` | cut -d, -f2)\ | grep gnome-terminal | cut -d, -f2
This solution feels the most robust to me. It recursively looks up the parent PID until finding one that belongs to GNOME Terminal.