code: def get_shell(pid=None, max_depth=6):
      
      if not pid:
          pid = os.getpid()
      processes = dict(_iter_process())
  
      def check_parent(pid, lvl=0):
          ppid = processes[pid].get('parent_pid')
          shell_name = _get_executable(processes.get(ppid))
          if shell_name in SHELL_NAMES:
              return (shell_name, processes[ppid]['executable'])
          if lvl >= max_depth:
              return None
          return check_parent(ppid, lvl=lvl + 1)
  
      shell_name = _get_executable(processes.get(pid))
      if shell_name in SHELL_NAMES:
          return (shell_name, processes[pid]['executable'])
      try:
          return check_parent(pid)
      except KeyError:
          return None
docs: Get the shell that the supplied pid or os.getpid() is running in.