- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Determining user's current shell
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 09:31 PM
тАО02-18-2002 09:31 PM
$shell isn't foolproof, and I'm sure there must be a more elegant alternative to running a csh-specific command and checking $?.
Any ideas?

Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 09:51 PM
тАО02-18-2002 09:51 PM
Re: Determining user's current shell
login shell can be extracted from /etc/passwd. For current shell, I prefer the easier way of checking the last access timestamp of the .profile, .cshrc, .bashrc, .dtprofile etc.
1) Check user profile access time, eg. for csh,
$ ls -lu .cshrc
- -rw-r--r-- 1 user1 users 814 Feb 18 20:02 .cshrc
$ csh
sys 21: ls -lu .cshrc
- -rw-r--r-- 1 user1 users 814 Feb 19 13:50 .cshrc
2) Use syntax testing:
For sh/ksh/bash,
# export a=1
For csh, tcsh,
# echo >/dev/null 2>&1
Ambiguous output redirect.
# echo $?
Variable syntax.
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 10:05 PM
тАО02-18-2002 10:05 PM
Re: Determining user's current shell
One issue with using the last access timestamps on .cshrc user profile is that the user can skip its execution (if he wants to) by executing:
# csh -f
Thus, a syntax check though more inconvenient is still more reliable:
For sh/ksh/bash:
# setenv
sh: setenv: not found.
For csh:
sys 21: setenv
setenv: Too few arguments.
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 10:12 PM
тАО02-18-2002 10:12 PM
Re: Determining user's current shell
The $SHELL variable only works for the login shell which can be extracted from /etc/passwd:
$ echo $SHELL
/usr/bin/sh
$ csh
sys 21: echo $SHELL
/usr/bin/sh
If you wish to prevent $SHELL from being tampered, you can add this in your /etc/profile between the traps:
readonly SHELL
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 10:42 PM
тАО02-18-2002 10:42 PM
Re: Determining user's current shell
how about:
env | egrep 'csh|ksh'
-Shabu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 10:53 PM
тАО02-18-2002 10:53 PM
Re: Determining user's current shell
I'm looking for current shell, not default shell.
To make matters worse, the user can have multiple telnet sessions open, each running a different shell.
Syntax-testing as suggested by Steven will definitely work, but I was looking for a more elegant solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 11:01 PM
тАО02-18-2002 11:01 PM
Re: Determining user's current shell
For different windows, if the prompt is different for each profile, .cshrc etc then the prompt can be used to identify the shell a user is current running, ie.
sh/ksh/bash:
$ ksh
$
$
csh/tcsh:
$ csh
sys 21:
sys 21:
In .cshrc,
set prompt = "$system \!: "
Hope this helps. Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 11:09 PM
тАО02-18-2002 11:09 PM
Re: Determining user's current shell
I think I have found the elegant solution you wanted:
Execute this:
# ps -fp `echo $$`
$ ksh
$ ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16088 16079 0 15:01:49 pts/0 0:00 ksh
$ csh
sys 21: ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16334 16088 0 15:06:28 pts/0 0:00 csh
sys 22: sh
$ ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16345 16334 0 15:06:40 pts/0 0:00 sh
You can thus incorporate this in your script eg:
shell_used=`ps -fp `echo $$`|tail -1|awk '{print $8}'`
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2002 11:18 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2002 12:07 AM
тАО02-19-2002 12:07 AM
Re: Determining user's current shell
That deserves a bunny :-)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2002 12:11 AM
тАО02-19-2002 12:11 AM
Re: Determining user's current shell
Probably best to just use "ps -p $$", then you don't have to worry about the timestamp being 1 or 2 fields long:
shell_used=`ps -p $$ | awk /:/'{print $4}'`
Rgds, Robin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2002 12:15 AM
тАО02-19-2002 12:15 AM
Re: Determining user's current shell
If you start from another shell or not.
shell in lowercase is set to /usr/bin/csh if you are using csh.
Otherwise if your shell has been open for a while there can be 9 parameters with the 9th as shell name
This works
#!/sbin/sh
MYSHELL=$(ps -fp $$|tail -1|sed -e 's/^.* //' -e 's/-//')
run as . showshell
Then MYSHELL is correct or adjust the previous
entry.
Steve Steel