LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-03-2005, 11:43 PM   #1
subu_s
Member
 
Registered: Jun 2004
Posts: 36

Rep: Reputation: 15
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
 
Old 01-04-2005, 12:06 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
How about which $SHELL
 
Old 01-04-2005, 12:11 AM   #3
subu_s
Member
 
Registered: Jun 2004
Posts: 36

Original Poster
Rep: Reputation: 15
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.
 
Old 01-04-2005, 02:03 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
 ps -o comm -p $$ | tail -1
 
Old 01-04-2005, 04:54 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 01-04-2005, 05:06 AM   #6
subu_s
Member
 
Registered: Jun 2004
Posts: 36

Original Poster
Rep: Reputation: 15
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
 
Old 01-04-2005, 05:53 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 01-04-2005, 05:59 AM   #8
zaicheke
Member
 
Registered: Apr 2004
Distribution: Slackware 10, Open BSD 3.6, Mac OS 10.3.7, Splack 10 beta
Posts: 393

Rep: Reputation: 30
echo $0 works for me.
 
Old 01-04-2005, 06:27 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Okay, I yield, that's seems unbeatable !
 
Old 01-04-2005, 06:40 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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,
 
Old 01-04-2005, 07:07 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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]

Last edited by druuna; 01-04-2005 at 07:10 AM.
 
Old 01-04-2005, 07:10 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell: Tar files: programmatically determining the name of the target directory stefanlasiewski Programming 3 08-17-2005 08:48 PM
determining shell abk4523 Linux - Newbie 2 06-09-2005 08:28 AM
Determining which shell is in use chakkerz Linux - Newbie 2 11-15-2004 05:47 PM
How to know what is the current (not initial) shell? rytrom Linux - Newbie 5 09-15-2004 01:17 PM
determining if shell is local connection or remote SaxyWeed Linux - General 3 01-26-2004 11:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 AM.

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