LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Determining current shell (https://www.linuxquestions.org/questions/programming-9/determining-current-shell-273542/)

subu_s 01-03-2005 11:43 PM

Determining current shell
 
Is there any straight forward way to determine the current shell?

$SHELL gives the login shell.

A descending sort of ps -aef and grepping one of sh, bash, csh, ksh can also give it, but that is programming.

Is there any command?

Thx in advance.

Regds,
Subu

homey 01-04-2005 12:06 AM

How about which $SHELL

subu_s 01-04-2005 12:11 AM

As I have mentioned in my original post, $SHELL gives the login shell.

e.g, if I login through bash and then do a 'csh' , the $SHELL is still 'bash' and not 'csh'.

That is the problem.

jlliagre 01-04-2005 02:03 AM

Code:

ps -o comm -p $$ | tail -1

bigearsbilly 01-04-2005 04:54 AM

I don't think there is a simple way
I've pondered this problem myself.
you'll have to use the 'ps' route.
It depends on which shell you are in at the time
as to how it behaves. So you can't rely on
consistent behaviour between different shells.

subu_s 01-04-2005 05:06 AM

I tested jlliagre's method and it works fine. Though it involves ps, it is a one line command and gives the desired o/p.

Thanks jlliagre

jlliagre 01-04-2005 05:53 AM

Quote:

you'll have to use the 'ps' route.
Well, here's a less portable (linux only) way, that avoid using 'ps'
Code:

cat /proc/$$/cmdline

zaicheke 01-04-2005 05:59 AM

echo $0 works for me.

jlliagre 01-04-2005 06:27 AM

Okay, I yield, that's seems unbeatable !

bigearsbilly 01-04-2005 06:40 AM

not so fast mr bond....


Code:


billym.primadtpliv>echo $0
-ksh
billym.primadtpliv>bash
billym.primadtpliv>echo $0
bash
billym.primadtpliv>csh
primadtpliv% echo $0
No file for $0


billy,

druuna 01-04-2005 07:07 AM

Hi,

Don't know if this works on a linux box, but on AIX it does:

echo $_

It even works for the csh shell :)

[edit]
Only works after the first 'switch'....... :(
[/edit]

bigearsbilly 01-04-2005 07:10 AM

AHA!
not so fast blofeldt!

(on solaris!)

Code:

primadtpliv% bash
bash: /opt/app/shared/env/profile: No such file or directory
bash
billrc file
billym.primadtpliv>echo $_
/export/home/billym/.billrc



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