title: How to close all terminal either running nothing or iPython from a bash script
body: <p>The number of terminals I run tends to proliferate, and most of them are not important, by which I mean they are running either nothing or iPython.</p>
  
  <p>In one of my previous questions, <a href="https://stackoverflow.com/questions/40406591/how-to-close-all-terminals-with-a-bash-script-that-effectively-presses-cntrlshi">How to close all terminals with a bash script that effectively presses Cntrl+Shift+Q in each terminal</a>, a user named <a href="https://stackoverflow.com/users/1563960/webb">webb</a> explained how to close all terminals with no subprocesses:</p>
  
  <pre><code>pgrep bash | xargs -n1 pstree -p -c | grep -v \- | grep -o '[0-9]\+' | xargs kill
  </code></pre>
  
  <p>I would like to adapt this bash script somewhat to also select the IDs of processes running iPython.</p>
  
  <p><strong>What I've tried so far</strong></p>
  
  <p>As an example, one output of</p>
  
  <pre><code>pgrep bash | xargs -n1 pstree -p -c
  </code></pre>
  
  <p>looks like this:</p>
  
  <pre><code>bash(793)───ipython(847)───{ipython}(854)
  bash(1787)───ipython(2061)───{ipython}(2062)
  bash(6018)───ipython(6052)───{ipython}(6053)
  bash(6810)───python(13796)───python(15653)───{python}(15657)
  bash(7197)───docker-compose(20923)───docker-compose(20924)─┬─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             ├─{docker-compose}(2+
                                                             └─{docker-compose}(2+
  bash(8629)───ipython(19746)─┬─{ipython}(19748)
                              ├─{ipython}(19762)
                              ├─{ipython}(19763)
                              ├─{ipython}(19764)
                              ├─{ipython}(19765)
                              ├─{ipython}(19766)
                              ├─{ipython}(19767)
                              └─{ipython}(19768)
  bash(9945)───ipython(31376)─┬─{ipython}(31377)
                              ├─{ipython}(31391)
                              ├─{ipython}(31392)
                              ├─{ipython}(31393)
                              ├─{ipython}(31394)
                              ├─{ipython}(31395)
                              ├─{ipython}(31396)
                              └─{ipython}(31397)
  bash(15906)───ipython(15974)───{ipython}(15976)
  bash(16302)───xargs(19015)───pstree(19027)
  bash(17377)───ipython(17407)───{ipython}(17409)
  bash(17797)
  bash(22210)───node(26380)─┬─{V8 WorkerThread}(26382)
                            ├─{V8 WorkerThread}(26383)
                            ├─{V8 WorkerThread}(26384)
                            ├─{V8 WorkerThread}(26385)
                            ├─{node}(26405)
                            ├─{node}(26406)
                            ├─{node}(26407)
                            └─{node}(26408)
  bash(32331)───ipython(32359)───{ipython}(32361)
  </code></pre>
  
  <p>I'd like to select the pids of processes running nothing - in this case <code>17797</code> - and processes running <code>ipython</code>. As I understand it, I would like to replace the command</p>
  
  <pre><code>grep -v \-
  </code></pre>
  
  <p>after the second pipe, which selects every line without hyphens in it, to a pattern which select either such lines or lines containing both <code>bash</code> and <code>ipython</code>. As a first step I tried (based on the tutorial <a href="https://www.cyberciti.biz/faq/grep-regular-expressions/" rel="nofollow noreferrer">https://www.cyberciti.biz/faq/grep-regular-expressions/</a>)</p>
  
  <pre><code>pgrep bash | xargs -n1 pstree -p -c | grep -v '(\-|ipython)'
  </code></pre>
  
  <p>but this seems not to work as the output contains the lines with <code>docker-compose</code>, for example. Any ideas on how to make this work?</p>