#!/bin/bash set -ue get_parent () { ps hoppid p$1 } get_children () { ps hopid --ppid "$1" } get_ancestors () { case $1 in 0|1|'') : ;; *) get_ancestors $(get_parent $1) esac echo "$1" } get_descendents () { for c in $(get_children "$1") do echo "$c" get_descendents "$c" done } declare -i p for p in "$@" do [[ $p -ne 0 ]] && { get_ancestors "$p" get_descendents "$p" } | { readarray -t pids ps -Hf "${pids[@]}" | sed "/^[^ ]* *$p /s/.*/&/" } done