Operating System - HP-UX
1819814 Members
3029 Online
109607 Solutions
New Discussion юеВ

Re: What command to determine the current shell?

 
Shawn_45
Occasional Advisor

What command to determine the current shell?

I have env variables and aliases in .cshrc but I wonder which file determines what default shell I use upon login?

Shawn
23 REPLIES 23
Pete Randall
Outstanding Contributor

Re: What command to determine the current shell?

Echo $SHELL


Pete

Pete
John Poff
Honored Contributor

Re: What command to determine the current shell?

Hi,

Your shell is set in /etc/passwd and exec'd when you login.

If you want to see what shell you are running, you can do:

echo $SHELL

[at least in ksh and POSIX sh, I'm not sure about csh].

JP
Jeff Schussele
Honored Contributor

Re: What command to determine the current shell?

Hi,

#echo $SHELL

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Ken Hubnik_2
Honored Contributor

Re: What command to determine the current shell?

It is usually specifies in the /etc/password file for each user what shell or script to execute at login.
James R. Ferguson
Acclaimed Contributor

Re: What command to determine the current shell?

Hi Shawn:

A user's default shell for login is defined in '/etc/passwd'.

For the c-shell, 'etc/csh.login' is sourced (read/executed) before '$HOME/.cshrc' and '$HOME/.login'.

Regards!

...JRF...
Helen French
Honored Contributor

Re: What command to determine the current shell?

Your default shell will be taken from the /etc/passwd file, as set when the account was created. Depend up on that values, it executes the proper profile files from your $HOME directory.

# echo $SHELL will tell you the current shell.
Life is a promise, fulfill it!
Vincent Fleming
Honored Contributor

Re: What command to determine the current shell?

By the way, it's the last column in /etc/passwd.

Good luck,

Vince
No matter where you go, there you are.
RAC_1
Honored Contributor

Re: What command to determine the current shell?

the shell you use is in /etc/passwd. However you can overide that by putting another shell in your profile.

This will spanwned by your shell in /etc/passwd.

echo $SHELL
There is no substitute to HARDWORK
Shannon Petry
Honored Contributor

Re: What command to determine the current shell?

Okay, I can login to a user account who has csh as the default shell.

I need to do something in a loop, so I run
% ksh
% echo $SHELL
/usr/bin/csh

Is there a way to do as the first question asks and get your current shell?


Regards,
Shannon
Microsoft. When do you want a virus today?
Paul Sperry
Honored Contributor

Re: What command to determine the current shell?

#env

and look for ENV=
Paul Sperry
Honored Contributor

Re: What command to determine the current shell?

#env

and look for SHELL=
Pete Randall
Outstanding Contributor

Re: What command to determine the current shell?

Shannon has an excellent point.

I've blindly assumed for years that $SHELL contains your current shell. It seems that that is not the case - it's the shell that you're assigned from /etc/passwd by login.

Hmmmmm!

Pete

Pete
MANOJ SRIVASTAVA
Honored Contributor

Re: What command to determine the current shell?

echo $SHELL

or use a typo like lsls and it will come back saying

sh : lsls not found

/bin/ksh : lsls not found



Manoj Srivastava

H.Merijn Brand (procura
Honored Contributor

Re: What command to determine the current shell?

All above answers are right, except for one thing: you cannot rely on $SHELL :)

I for one have a tcsh for logon (as all state, the last field in the /etc/passwd file), but I've set my $SHELL environment to /usr/bin/sh for all the obvious reasons.

Fast way to check the last field of /etc/passwd

# grep "^`logname`:" /etc/passwd | sed 's/.*://'

do NOT rely on $LOGNAME, which is easy to overrule.

If you want to know for the /effective/ userid:

# perl -le'print+(getpwuid$>)[8]'

And, heh, that's even shorter than grep/sed, *and* more reliable. Now you've got easyEnjoy, have FUN! H.Merijnctionality *and* fun in one blow :)

and for the /real/ uid:

# perl -le'print+(getpwuid$<)[8]'


Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Shannon Petry
Honored Contributor

Re: What command to determine the current shell?

Good call on it not being reliable. As far as I know, it will show your login shell, and never your current shell.

Further, there is no way that I know of to get your "current" shell, only the login shell.

One of them things I always wondered about.. figure it would be set somewhere, but.... maybe just me.

regards,
Shannon
Microsoft. When do you want a virus today?
Rodney Hills
Honored Contributor

Re: What command to determine the current shell?

If you have "lsof", you could do the following-

currshell=`lsof -p $$ | awk '$4=="txt"{print $9}'`

This will give the path to the shell you are running (given by $$).

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: What command to determine the current shell?

Okay guys, how about this which should work in any shell including csh.

echo $$ | tail -1 | awk '{print $NF}'
If it ain't broke, I can fix that.
Leif Halvarsson_2
Honored Contributor

Re: What command to determine the current shell?

Hi,
Try this:


# ps |grep $$
10956 pts/0 0:00 csh
# ksh
# ps |grep $$
10963 pts/0 0:00 ksh
#



A. Clay Stephenson
Acclaimed Contributor

Re: What command to determine the current shell?

Stupid me: I typed the correct command at my terminal but entered the wrong command on the Forums. It should be: ps -p $$ | tail -1 | awk '{print $NF}'

Since ps -p $$ will only list the current process.
If it ain't broke, I can fix that.
Gregory Fruth
Esteemed Contributor

Re: What command to determine the current shell?

To determine the current shell (as opposed
to the login shell), try "echo $0":

sh$ echo $0
sh

ksh$ echo $0
ksh

tcsh% echo $0
tcsh

csh% echo $0
No file for $0.

Unfortunately, csh seems to be the oddball.
Perhaps you could test $status after you
try the echo. If $status is 1, then you're
probably in csh. (I can't figure out how
to suppress the "No file for $0" message
or send it to /dev/null; chalk it up to csh's
weirdness.)

Just out of curiosity, why do you want to
determine the current shell? I can't think
of a good reason why one would do this.
Normally you're in the shell specified by
SHELL unless you started a new shell,
in which case you ought to know what
shell you're in, because you're the one
who changed it!
Rodney Hills
Honored Contributor

Re: What command to determine the current shell?

Gregory,

Unfortunatelly your solution won't work inside a running script.

-- Rod Hills
There be dragons...
Bill Hassell
Honored Contributor

Re: What command to determine the current shell?

One of the most important shell scripting techniques is to always define how the script is to be run. Line #1 should always be #! followed by the full pathname of the desired shell. This is also known as the courtesy loader or shell loader. Script writers that use Perl must always use the line since none of the shells know anything about Perl. In general, knowing the current shell is not too important as long as scripts define what they need in line #1.


Bill Hassell, sysadmin
Gregory Fruth
Esteemed Contributor

Re: What command to determine the current shell?

The echo $0 stuff is for determining
the current shell interactively. In a
script, finding the current shell is
pointless, because the shell should
be set on the #! line. If it isn't set,
then exec() decides what to do
with your script. The man page
for exec() (HP-UX 10.20) says:

"If file does not have an executable
magic number (see magic(4)), it is
passed to the Bourne shell as a
shell script."

You can test this by saving these
lines to a file, then doing a "chmod
a+x" on the file. Then run the
script by typing its name.

echo `eval "if (1 == 1) then; echo 'this is a C-ish shell'; endif"`
echo `eval "[[ 1 = 1 ]] && echo 'this is a POSIX-ish shell'"`
echo `eval "[ 1 = 1 ] && echo 'this is a Bourne-ish shell'"`

If the script is running under csh or
tcsh, you'll see the first message,
followed by two errors.

If the script is running under ksh or
the POSIX sh, you'll see an error
followed by the second and third
messages.

If the script is running under the
Bourne sh, you'll see two errors,
followed by the third message.

The man page for exec() seems to be
out of date, because it evidently passes
the script to /usr/bin/sh, the POSIX shell.

But then again, this is all pretty pointless.