title: How to list great-grandchildren PIDs of target PID from a Bash script?
body: <p>I need to write script which displays the PID's of all the great-grandchildren of the processes whose PID's are given as argument.</p>
  
  <p>The following script displays grandchildren of the processes whose PID's are given as argument but I need great-grandchildren.</p>
  
  <pre><code>#!/bin/env bash
  
  display_cpid() {
      local depth=$1 pid=$2 child_pid
      (( ++depth ))
      while IFS= read -r child_pid; do
          if (( depth &lt; 2 )); then
              display_cpid "$depth" "$child_pid"
          else
              echo "$child_pid"
          fi
      done &lt; &lt;(pgrep -P "$pid" | xargs)
  }
  
  display_cpid 0 "$1"
  </code></pre>
  
  <p>I expected script displays great-grandchildren but it displays grandchildren.</p>