From a bash or sh shell, how can I determine if it was called with the bash or sh command, a login shell, an xterm, and, in the case of the former, how was that called?For example, if I call bash from an xterm, and then call it again, inside that instance, it might output something likeme@mylinuxmachine:~$ bashme@mylinuxmachine:~$ bashme@mylinuxmachine:~$ magic_commandCalled by /bin/bash {    Called by /bin/bash {        Called by xterm    }}
Determine how bash or sh was called
bash;shell;command line;process;bourne shell
You may use pstree for this:$ bashbash-4.4$ pstree -p $$-+= 00001 root /sbin/init \-+= 85460 kk tmux: server (/tmp/tmux-1000/default) (tmux)   \-+= 96572 kk -ksh93 (ksh93)     \-+= 72474 kk bash       \-+= 14184 kk pstree -p 72474         \-+- 51965 kk sh -c ps -kaxwwo user,pid,ppid,pgid,command           \--- 91001 kk ps -kaxwwo userThe pstree utility will show the parent-child relationships for all processes currently running on the system.  With -p $$ you restrict its output to only contain processes related the the current shell (whose process ID is stored in the $ variable).To cut the output off at the point where it gets to the current shell, you could use sed:bash-4.4$ pstree -p $$ | sed /= $$ /q-+= 00001 root /sbin/init \-+= 85460 kk tmux: server (/tmp/tmux-1000/default) (tmux)   \-+= 96572 kk -ksh93 (ksh93)     \-+= 72474 kk bashFor Linux systems, which apparently use a different implementation of this utility from what I'm using (on OpenBSD), you may want to use$ pstree -salup $$to get a similar output, and$ pstree -salup $$ | sed /,$$\$/qto cut the output off at the point where it gets to the current shell.Here's a shell function pls (for process ls, that's the best I could come up with) that does the above for any given PID (or the PID of the current shell if left out):function pls{  local pid=${1:-$$}  pstree -salup $pid | sed /,$pid\$/q}