Skip to main content
1 of 2

Outside of Linux land or lacking access to the /proc filesystem or equivelent, you can make use of pstree:

Assuming you have the pid of

On a Mac:

./test.sh 
16012
-+= 00001 root /sbin/launchd
 \-+= 00245 wingwong /sbin/launchd
   \-+= 04670 wingwong /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -psn_0_2052597
     \-+= 11816 root login -pf wingwong
       \-+= 11817 wingwong -bash
         \-+= 16012 wingwong ksh ./test.sh
           \-+- 16013 wingwong pstree -p 16012

On a Linux box:

./test.sh 
14981
bash(14981)---pstree(14982)

The format and style of the output from pstree differs, depending on your environment, but you can enforce ASCII output and then sed/tr/awk/etc. filter the output to get the shell that is running the script.