JP Vossen on 22 Jan 2009 13:52:24 -0800

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] What is my current shell?

> Date: Thu, 22 Jan 2009 14:16:02 -0500
> From: TuskenTower <>
> We have an oddball problem.  Is there any portable way to determine
> what shell you are using once you are inside a script?

Doing "portable" with shell scripts is really hard. :-(  The GNU utils 
are partly responsible for this, since they spoil you on Linux.  Your 
best bet is to develop on the oldest, crappiest, least common 
denominator system you can find, and go from there.  If you try to do it 
on Linux, you will paint yourself into a lot of corners.

> Right now, we are thinking of using
> which `echo $0`

That works at the CLI but not inside a script; I just get the name of 
the shell script.

You can take a look here, but nothing I tried really worked either:

Obviously that's bash, but much of bash behavior is shared by the Bourne 

> As you can see here, $SHELL does not change when you switch shells.
> [2:07pm] [shaha:pts/8] [shaha] : /gtc/staff/shaha/work/sortT > echo $SHELL
> /usr/local/bin/tcsh
> [2:08pm] [shaha:pts/8] [shaha] : /gtc/staff/shaha/work/sortT > bash
> shaha@shaha:~/work/sortT$  echo $SHELL
> /usr/local/bin/tcsh

Huh, I'd have thought $SHELL would work.  Bummer.

This works on Linux (hardy and etch) and Solaris 9i:
	ps -f -p $$ | tail -1 | awk '{print $8}'

But the 'ps' command is one of the ones that really, really, SUCKS for 
portability and this may not work on other systems.  I talk about 
portability issues a little in recipe 1.15 and 15.1-6 of the _bash 
Cookbook_.  VMware (or whatever) is your friend, I have Solaris (Intel) 
8,9,10, {Free,Net,Open}BSD.  But you are toast for AIX and HP-UX since 
those don't (didn't?) run on x86. might be useful for 
testing those. was shut down effective September 
30, 2008 and replaced by a new virtual program.  It is not clear to me 
if there is a charge to sign up for it.

I haven't tested that ps command on anything but what is listed above. 
And it seems really silly that there isn't a better way, but I can't 
think of one at the moment.  I'd say a perl one-liner but haven't 
thought of that yet either.

(Ah, I just saw more inclusive answer along the same lines from Mark. 

> This is for problems at customer sites where they are using the wrong
> Bourne shell on Solaris (/bin/sh) when we want them to use
> /usr/xpg4/bin/sh.

Depending on what you are doing and how much control you have of the 
scripts and environment, this may or may not help, but recipe 15.2 talks 
about using the getconf tool to set a POSIX path.  Here is what you get 
on my Solaris 9i VM:
	bash-2.05$ getconf PATH

On Thu, Jan 22, 2009 at 4:05 PM, Eric <> wrote:
 > > Try this:
 > > ps a | grep "^ *$$" | grep -v grep | awk '{print $5}'

I don't see how to do it in this case, but just as a general note, 
instead of the ugly '| grep -v grep |' hack, you can use a regex in the 
search field.  For example, to find all SSH processes (Linux):
	ps auwx | grep '[s]sh'

That works because grep interprets '[s]sh' as "ssh" and that doesn't 
match '[s]sh' so you get what you meant without the '| grep -v grep |' 
hack.  I confess I don't see a way around that hack in this case though.

JP Vossen, CISSP            |:::======|
My Account, My Opinions     |=========|
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.

Philadelphia Linux Users Group         --
Announcements -
General Discussion  --