How do I check which shell I am using?
Please note that terminal is interface to the shell (which at one point used to be actually physical interface), and shell is not terminal — it is a command interpreter. See also askubuntu.com/a/640105/295286
9 Answers 9
You can type the following command in your terminal to see which shell you are using:
The result will look something similar to the below if you are using the bash (Bourne Again Shell) terminal:
@Mi_Onim $0 is the name of the running process. If you use it inside of a shell then it will return the name of the shell. If you use it inside of a script, it will be the name of the script.
@Anwar makes a good point, after you get the shell name from echo $0 , ls -l `which
To find the shell you have on the default environment you can check the value of the SHELL environment variable:
To find the current shell instance, look for the process (shell) having the PID of the current shell instance.
To find the PID of the current instance of shell:
Now to find the process having the PID:
$SHELL is the default shell for the system (or user), which is usually (but not necessarily) the shell that is actually being used at any given moment.
echo $SHELL gave me /bin/csh and ps -p $$ gave me 22673 pts/1 00:00:00 bash . Kingmilo explained (above) why they are not the same.
To grab ONLY the shell name for the current shell in a way portable between gnu & bsd ps versions, this works well: ps -cp «$$» -o command=»»
$SHELL gives you the default shell. $0 gives you the current shell.
For example: I have bash as my default shell, which I use for my Terminal App. But for my iTerm2 app, I use the command as the window opens: /bin/ksh .
So my $0 gives me /bin/ksh on iTerm2. $SHELL gives me /bin/bash on iTerm2. $0 , $SHELL gives me /bin/bash on Terminal
For me, I have zsh installed and it’s the current default, to change to bash, all I have to do type bash in the terminal. To switch back to zsh, type zsh
You don’t want to keep going back and forth that way because you stack up shell within shell within shell, and a fresh context within each. In general it’s best to type ctrl-d or exit to return to the previous shell.
Best answer, should be chosen as answer. Agree to yousuf Azad on changing shell, but also agree to stacking issue indicated by Phill Apley. My default is also zsh , but to switch I type bash , and to return back I can simply do exit or ctrl + D
The other answers tend to be using shell specific features, but we are trying to discover which shell we are using, so they assume the answer to the problem. For example none of the answers will work on fish.
sh -c 'ps -p $$ -o ppid=' | xargs ps -o comm= -p
Instead use the $$ syntax in an invocation of sh, but then we are looking for the PPID not the PID. Use the PPID to find the cmd.
sh -c 'ps -p $$ -o ppid=' | xargs -I'<>' readlink -f '/proc/<>/exe'
Thanks for improvement @muru
You ca use ppid= / cmd= to omit the headers (and so the tail -1 s), and consider looking at /proc/. /exe to see what file is being run (since the cmd output can be manipulated by whatever ran the shell).
Apparently this does not work in busybox, due to non posix compliance: ref: github.com/broadinstitute/cromwell/pull/…
this solution solves the problem of assuming the shell but introduces the problem of assuming the OS. The BSD version of readlink shipped with macOS (and presumably other BSD Distros) uses -f to specify «format» which is a printf style string that must start and end with % and has a variety of templating options.
Great answer, @EvanBenn, thanks! Here is a tiny modification to allow it to work with shells like BusyBox , where the -p arg to ps isn’t supported, and where there aren’t a whole lot of other utilities like cut to help. Code: set — $(ps -o pid,ppid | grep -E «^ *$$»); readlink -f «/proc/$<2>/exe» . I’m using your method to determine the shells used on a bunch of «free online Linux shell» websites, and it’s great!2>
The original post asked three questions. The answers given do cover the first question, «When I open a terminal window, which shell is opened by default?» They also answer a question which was NOT asked, namely «How can I find out which shell is currently running in the terminal?» However, as far as I can see nobody has answered either the second or third questions originally asked, namely «How do I check how many shells are installed?» and «How do I change the shell used from my account?»
- To answer «How do I check how many shells are installed?» the following command will list all the available shells:
# /etc/shells: valid login shells /bin/sh /bin/dash /bin/bash /bin/rbash
To know which is the default shell for your user, you can run:
For example if you’re using Bash you should get the following output:
If you didn’t change any configuration it should be Bash since Bash it’s the default shell on Ubuntu.
@kol there was before edit. Please also note $SHELL is the default shell for the system (or user), which is usually (but not necessarily) the shell that is actually being used at any given moment.
@kingmilo Reading the answer again i think i understand what is your concern, probably i didn’t express myself well. I clarified the meaning of «currently» in my answer
@frederickjh fish is a bit of an odd one out here. The $$ variable is actually defined by POSIX and will work on the vast majority of shells. Fish has decided not to follow the standard here so I think it’s fair to ignore it. I can confirm that $$ works as expected in sh, dash, bash, zsh, ksh, ash, tcsh and csh. In fact, off the top of my head I can’t think of any other shell except fish where it doesn’t work.
You may not want to know the current shell’s name (e.g. -bash , bash , zsh , etc., from echo $0 ), nor default shell’s executable path (from echo $SHELL ), but rather the current shell’s executable path (especially useful e.g. if you have more than one version of Bash installed).
To do this you can use lsof -p «$$» or with some extra coding to extract just the required info:
lsof -p "$$" | grep -m 1 txt | xargs -n 1 | tail -n 1
Example output for Bash installed via Homebrew:
/usr/local/Cellar/bash/5.1.8/bin/bash
The above is different from echo $SHELL , both because the above is for the shell which is currently running rather than the user’s default shell, and also because the above gives the executable after any symlinks have been expanded. E.g. for the same Bash install as above, echo $SHELL gives /usr/local/bin/bash .
EDIT: If you need to allow for possible space characters in the shell’s path, use lsof -p «$$» | grep -m 1 txt | xargs -n 1 | tail -n +9 | xargs instead.
To address your third question, «How do I change the shell used from my account?», the answer is to use chsh .
You will use a program called chsh. There is a interactive method and non-interactive method. Type the following into your terminal:
INTERACTIVE METHOD
This results in a brief dialog in which the user is prompted first for their password and then for the full path of the desired new shell.
Caution should be exercised when changing one’s default shell because it is possible to make an error that only the root user (i.e., system administrator) can repair (although it should be easy for a skilled user to repair it on a home system). In particular, it is important to first test the shell temporarily in the current session and then to make certain that a valid shell name is being entered when making the permanent change.
NON-INTERACTIVE METHOD
I will use csh as again an example.
The -s sets it for you without having to go into the editor to do it.
Once this is executed successfully, then echo $SHELL will still say that you are in the same shell as before. However, you need to log out and back in for the change to take effect. Now do echo $SHELL . You should see it shows the new shell.
How to Find Which Shell You Are Using on Linux
Here are four simple commands to find out which shell are you using in Linux.
Which Shell am I using in Linux? Is that even a question? Of course, it is. There are several shell available for Linux systems. Some of the most popular ones are:
You may wonder this in a few situations.
For example, if you log into a Linux system not known to you, you may wonder which shell is being used by default. If you frequently change shell in Linux, you might wonder which shell you are using at the moment.
Let me show you various Linux commands to find out which shell you are using.
Find out which shell you are using in Linux
Now there is no command that will give you this output with 100% accuracy for all the shells. But these commands should be accurate for most of the shells.
Method 1
You can use the special shell parameter $$. “$$” indicates the process id of the current instance of the shell you are running. This is a read-only parameter and cannot be modified.
If you try to see the process information, it should show you the process name i.e. the shell here.
The output should be like this:
PID TTY TIME CMD 15012 pts/0 00:00:00 zsh
Method 2
You can also use $0. $0 can be the name of the shell or the name of shell script. When it is used inside a shell script, it denotes the name of the script.
But if you use it in a shell without filename, it will show the name of the shell.
The output will simply have the name of the shell:
Method 3
You can also use the pstree command. pstree means process tree and it shows all the running processes as a tree.
If you provide it with no argument, it will show all the processes from init or systemd.
However, if you give it a process id, it will show all that processes as the root of the tree. In other words, it will show all the processes initiated by that process.
You can use the same $$ bash parameter we saw in method 1.
Method 4
The last method is using the files in proc directory. If you read the article about checking CPU info in Linux, you probably already know that this directory contains the runtime system information about your Linux system.
You can use the following command to get the shell you are using:
The output should give you the name of the shell.
Bonus Tip: Know the version of the shell
So you learned how to know which shell you are using. What about the version of the shell you are using? You can get the version of the shell simply by adding –version after the name of your running shell.
For example, if you are running zsh, you can use this:
The output will give you the version information:
zsh 5.4.2 (x86_64-ubuntu-linux-gnu)
I hope this quick tutorial helped you in finding out which shell you are running. Don’t forget to check out another simple tip on changing shell in Linux.
By the way, do you use some other way to check which shell you are using? Why not share it with us in the comment section?
How can I know which shell I am using?
I am writing a shell script. The tutorial that I am reading have the first line like this : #!/usr/bin/env bash/ but it isn’t working for me. ( error : no such directory ) How can I find out which bash I am using and where is it located? Appreciate for any advice and help. Thanks a lot. It works now. solution is #!/usr/bin/env bash Another problem: Why it just can’t read the word ‘restart’ my code in the start.sh:
#!/usr/bin/env bash/ RESTART="apachectl restart" $RESTART
Usage: /usr/local/apache2/bin/httpd [-D name] [-d directory] [-f file] [-C "directive"] [-c "directive"] [-k start|restart|graceful|graceful-stop|sto p] [-v] [-V] [-h] [-l] [-L] [-t] [-S] Options: -D name : define a name for use in directives -d directory : specify an alternate initial ServerRoot -f file : specify an alternate ServerConfigFile -C "directive" : process directive before reading config files -c "directive" : process directive after reading config files -e level : show startup errors of level (see LogLevel) -E file : log startup errors to file -v : show version number -V : show compile settings -h : list available command line options (this page) -l : list compiled in modules -L : list available configuration directives -t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings) -S : a synonym for -t -D DUMP_VHOSTS -t -D DUMP_MODULES : show all loaded modules -M : a synonym for -t -D DUMP_MODULES -t : run syntax check for config files
why is it like that? it seems that it can read the word restart. Thank you all! I have fixed it now. solution: edit the file in unix (vim/nano and whatever but not in windows)