It turns out there’s a bug, due primarily to testing on zsh and putting the script into bash.
function find_lowest_subprocess() { local -i parent=$1 local pids typeset -a pids pids=($(pgrep -P $parent)) while ((${#pids[@]} > 0)); do if ((${#pids[@]} > 1)); then parent=${pids[0]} local i=0 while ((i < ${#pids[@]})); do local sub=$(pgrep -P ${pids[$i]}) [[ -n $sub ]] && parent=$sub ((i=i + 1)) done else parent=$pids fi pids=($(pgrep -P $parent)) done echo $parent }
Someone, somewhere forgot about the parenthes around the array assignments. Shame on me!