Which shell am I running on?

When I run echo $SHELL the output says /bin/tcsh which means that I am running a tcsh shell.
But for example when I issue the following command

alias emacs 'emacs -nw'

I get the following error:

bash: alias: emacs: not found
bash: alias: emacs -nw: not found

and when I issue alias emacs="emacs -nw" it runs fine!

This is confusing since I am running tcsh but the commands are interpreted by bash.

What could be the reason?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

$SHELL is not necessarily your current shell, it is the default login shell. To check the shell you are using, try

ps $$

This should work on most recent Unix/Linux with a ps that supports the BSD syntax. Otherwise, this is the portable (POSIX) way

ps -p $$

That should return something like this if you are running tcsh:

8773 pts/10   00:00:00 tcsh

If you want to have tcsh be your default shell, use chsh to set it.

Method 2

From the command line, you can also use the $0 variable to determine which shell you are using. e.g.:

~$ echo $0
/bin/bash


~$ ksh
$ echo $0
ksh

Note: you cannot determine the shell using $0 within a script, because $0 will be the script itself.

Method 3

This is an amendment to all of the better answers above. I had a tiny issue identifying dash at one point; seemed right to share:

curl -fsSL http://www.in-ulm.de/~mascheck/various/whatshell/whatshell.sh | sh
ash (dash 0.5.5.1 ff)

curl -fsSL http://www.in-ulm.de/~mascheck/various/whatshell/whatshell.sh | bash
bash 4.3.30(1)-release

Good for troubleshooting in tight spots is all. Cheers.

Method 4

This works for linux:

lsof -a -p $$ -d txt

or, if you need a stronger selection (more systems):

lsof -a +D /bin +D /usr/bin -p $$ -d txt

Method 5

A Unix/Linux Operating System may have more than one shell installed.
You can type the following command in your terminal to see which shell you are using:

Below is the list,

  • Bourne Shell – sh
  • C Shell – csh
  • T Shell – tcsh
  • Korn Shell – ksh
  • Bourn-again Shell – bash
  • Dash Shell
  • Z shell

This list is not complete and there are many other shells.

/etc/shells file stores list of all shells installed in your system. Simply, run the below command to find out the installed shells,

$ cat /etc/shells

Run grep command on /etc/passwd file to know the default shell when you log in to Unix or Linux operating system.

$ grep john /etc/passwd

You can also use the $SHELL variable for the same purpose.

echo $0
$ echo $SHELL

The result will look something similar to the below if you are using the bash (Bourne Again Shell) terminal:
When there are multiple shells installed on your system, you can always switch the shell.

so how do you know which shell you are running at the moment?

Simply use the below command

-bash
$ ps -p $$

I think all your questions are answered. If you still have a doubt, you can refer to this source article.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x