ZSH_PID

As of Zsh 5.9, there is no official $ZSH_PID variable documented in the Zsh manual. Zsh does not have a direct equivalent to Bash's $BASHPID ($BASHPID), which dynamically reflects the process ID of the sub-shell that is running.

The lack of subprocess id presents a challenge in more advanced use cases when working with Zsh, specifically when determining whether the current shell is a subshell. In Zsh, there is no built-in equivalent to Bash's $BASHPID, which dynamically updates to reflect the process ID of subshells. This limitation makes it difficult to programmatically decide whether to send messages to the parent process, as we cannot easily determine if we are operating within a subshell.

Attempt to get sub-shell PID

foo() {
    echo ""
    echo "## ${1:?how its called}"
    echo "Inside foo() - \$\$=$$"
}

echo "Main Zsh PID using \$\$=$$"

# shellcheck disable=SC2016
capture="$(foo 'Called foo() inside subshell $()')"
echo "${capture:?}"
foo "Called foo() directly from main"

Output

Main Zsh PID using $$=48046

## Called foo() inside subshell $()
Inside foo() - $$=48046

## Called foo() directly from main
Inside foo() - $$=48046