LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-04-2011, 09:17 AM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
What Shell Are You Using Currently?


Hi,

Is there anyway to find out what shell we are currently using?

For example, I logged in and issued the following command to check my current shell:

Code:
%echo $SHELL
/bin/sh
then I changed to bash by executing the following command and checked again as to what shell I changed to:

Code:
%bash
[devarish@m-net ~]$ echo $SHELL
/bin/sh
but I got the same output, i.e. /bin/sh.

Whereas the entry in /etc/passwd says a different story:

Code:
[devarish@m-net ~]$ grep ^$USER /etc/passwd
devarish:*:68166:10:Devarishi Kumar Mahadeva:/home/devarish:/usr/local/bin/menu
[devarish@m-net ~]$

So, how can we make sure from within a shell script that at any particular time or while that script is executing as to what shell the logged-in user is currently working in because echo $SHELL reports the same output even when the user has changed to some other shell temporarily?

Please note that we are not issuing the chsh command to change the shell permanently.
 
Old 01-04-2011, 09:36 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
ps hp $$ | awk '{print $NF}'
 
1 members found this post helpful.
Old 01-04-2011, 09:59 AM   #3
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by pwc101 View Post
Code:
ps hp $$ | awk '{print $NF}'
My question was long, your answer was short!

[1]

Code:
-bash-2.05b# ps p $$
  PID TTY      STAT   TIME COMMAND
 1389 pts/0    S      0:00 -bash
-bash-2.05b# ps p $$ | awk '{print $NF}'
COMMAND
-bash
[2]

Code:
-bash-2.05b# sh
sh-2.05b# ps p $$
  PID TTY      STAT   TIME COMMAND
 1827 pts/0    S      0:00 sh
sh-2.05b#
Thanks, Guru! I got it that echo $$ reports the current PID and when it is run on the Shell Prompt then it is the PID of the currently running Shell.
 
Old 01-04-2011, 10:16 AM   #4
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Please see that when we change our current Shell to some other Shell then our Shell Script stops functioning as soon as it changes the current Shell and it resumes as soon as we exit from the changed Shell:

Source Script:
Code:
-bash-2.05b# cat > chkShell.sh
#!/bin/bash
echo "Parent Process:"
ps p $PPID
echo "Current Process:"
ps p $$
echo "Changing Shell to sh..."
sh
echo "Let us see where we are now:"
ps p $$
-bash-2.05b# chmod 755 chkShell.sh
Sample Run of the Script:

Code:
-bash-2.05b# chkShell.sh
Parent Process:
  PID TTY      STAT   TIME COMMAND
 1833 pts/0    S      0:00 -bash
Current Process:
  PID TTY      STAT   TIME COMMAND
 1961 pts/0    S      0:00 /bin/bash /root/bin/chkShell.sh
Changing Shell to sh...
sh-2.05b# echo $SHELL
/bin/bash
sh-2.05b# ps p $$
  PID TTY      STAT   TIME COMMAND
 1964 pts/0    S      0:00 sh
sh-2.05b# exit
exit
Let us see where we are now:
  PID TTY      STAT   TIME COMMAND
 1961 pts/0    R      0:00 /bin/bash /root/bin/chkShell.sh
-bash-2.05b#
So, how can we get out of the changed Shell from within the script itself without having to type "exit" at the command prompt?

I tried "exit" in the script but it didn't work. "return" would work only in a function and a sourced script.

Any ideas?
 
Old 01-04-2011, 10:59 AM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Err... I can't think of a way of doing this. Sorry.

edit: You may have better luck using expect.

Last edited by pwc101; 01-04-2011 at 11:00 AM.
 
  


Reply



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
How to run root privileged Linux command as normal user via shell shell tcegrid Linux - Newbie 1 06-23-2008 03:38 PM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
LXer: Shell tip: Set the shell prompt and themes in Linux Terminal LXer Syndicated Linux News 0 06-12-2007 03:02 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:48 PM.

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
Open Source Consulting | Domain Registration