LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-18-2007, 10:43 AM   #1
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
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
 
Old 01-18-2007, 10:53 AM   #2
olaola
Member
 
Registered: Aug 2006
Location: Italy
Distribution: Fedora
Posts: 41

Rep: Reputation: 15
echo $SHELL
 
Old 01-18-2007, 10:56 AM   #3
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Original Poster
Rep: Reputation: 30
neh... echo $SHELL is giving me the default shell, bash that is
 
Old 01-18-2007, 11:04 AM   #4
olaola
Member
 
Registered: Aug 2006
Location: Italy
Distribution: Fedora
Posts: 41

Rep: Reputation: 15
you're right, try with:

echo $shell

Last edited by olaola; 01-18-2007 at 11:07 AM.
 
Old 01-18-2007, 11:04 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
echo $0

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

Last edited by colucix; 01-18-2007 at 11:09 AM.
 
Old 01-18-2007, 01:31 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
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]

Last edited by unSpawn; 01-18-2007 at 01:52 PM. Reason: Dunno. Can I buy a vowel now?
 
Old 01-20-2007, 08:45 AM   #7
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
init Parent Process ID sobia Linux - General 1 06-09-2006 11:28 PM
! HELP ! Script : How to control parent shell ??? fawqati Programming 1 06-03-2006 01:58 PM
getting parent process ID in sh pradeepp Programming 2 09-22-2005 07:13 AM
Squid Authentication via Parent tombarnfield Linux - Newbie 3 06-27-2005 11:35 AM
Parent PIDs Sinope Linux - Newbie 6 10-06-2004 12:46 AM


All times are GMT -5. The time now is 06:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration