I am trying to get the pid of a currently executing subshell - but $$ is only returning the parent pid:
#!/usr/bin/sh
x() {
echo \"I am a sub
If you use Linux-kernel, you can use Linux-kernel's /proc/self feature to do this:
In simplest form: cd -P /proc/self && basename "${PWD}"
To keep the PWD and OLDPWD variable: PWD_BACKUP="${PWD}";OLDPWD_BACKUP="${OLDPWD}";cd -P /proc/self && basename "${PWD}";cd "${PWD_BACKUP}";OLDPWD="${OLDPWD_BACKUP}"
For example:
$ cd -P /proc/self && basename "${PWD}"
26758
$ (cd -P /proc/self && basename "${PWD}")
26959
$ (cd -P /proc/self && basename "${PWD}")
26961
$