LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to know which is the parent shell? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-know-which-is-the-parent-shell-520521/)

anupamsr 01-18-2007 09:43 AM

How to know which is the parent shell?
 
Hi!

I work on a netwerk, with the home directory mounted as NFS. I usually work on ZSH but since some of the computers dont have it, and I occasionally need to ssh to it, my shell is BASH, and .bashrc calls a zsh.

I want to know, if there is a way to check which shell is running currently? I mean, which shell is running the shell script?

This is what I want:
Code:

$ cat ./script
if (current shell == zsh) then
  echo "zsh is running"
else
  echo "something else is running"
fi

# better will be:
# echo "$current_shell is running"

Please help... I am stuck :(

olaola 01-18-2007 09:53 AM

echo $SHELL

anupamsr 01-18-2007 09:56 AM

neh... echo $SHELL is giving me the default shell, bash that is

olaola 01-18-2007 10:04 AM

you're right, try with:

echo $shell

colucix 01-18-2007 10:04 AM

echo $0

should do the trick... but from the command line only. In a script it returns the name of the script itself.

unSpawn 01-18-2007 12:31 PM

Minor nit: the "$SHELL" variable isnt set by default by all shells (Ksh, IIRC) and it's not always a readonly variable. Shells have quirks and checking differences is one way to test: for a simple Bash vs Zsh you could work with some nested stuff Bash can't do. Another (more generic and compatible) way could be to look at the process itself:
Code:

shellname() { i=`readlink -f /proc/$$/exe`; case "$i" in */bash) echo "$i";; *) echo "Booh, hiss!";; esac; }
This works from the CLI and in Ash, Bash, Jsh, and Ksh scripts, but not in Tcsh (variable assignment).

[edit]
Looking at the title of the thread "How to know which is the parent shell" Ash, Bash and Ksh (but not Jsh) support $PPID. If there's no Parent PID or if it's not readonly you could:
Code:

_PPID=`/bin/ps -eopid,ppid|grep "^$$"|awk '{print $2}'`; readlink -f /proc/"$_PPID"/exe
[/edit]

anupamsr 01-20-2007 07:45 AM

Thanks. I unfortunately unsubscribed this thread somehow so couldn't read all the answers (duh).

Anyway, seems like "echo $0" tells the current shell. Also, I found something like shellname() in /etc/profile of Suse9.3
Code:

if test -f /proc/mounts ; then
  case "`/bin/ls -l /proc/$$/exe`" in
    */bash)    is=bash ;;
    */rbash)    is=bash ;;
    */ash)      is=ash  ;;
    */ksh)      is=ksh  ;;
    */zsh)      is=zsh  ;;
    */*)        is=sh  ;;
  esac
else
  is=sh
fi

So $is in Suse9.3 tells the same.

_PPID=`/bin/ps -eopid,ppid|grep "^$$"|awk '{print $2}'`; readlink -f /proc/"$_PPID"/exe

tells me the parent shell, which actually I was asking. Thanks :)


All times are GMT -5. The time now is 04:46 PM.