Linux terminal which shell

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 see if it is symlinked to another shell binary.

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

Читайте также:  Config bind on linux

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!

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).

Читайте также:  Linux mint conky theme

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.

Источник

What is default shell for terminal?

Let’s say, I opened a terminal and entered / executed some shell commands. But I didn’t invoke explicitly Bash or any other shell. What shell was used by default?

5 Answers 5

The one specified on your line in /etc/passwd (it is a : separated line and the shell is the final one).

chris:x:1000:1000:Chris. /home/chris:/bin/bash 

Here it is /bin/bash (the Ubuntu default)

$ chsh Password: Changing the login shell for chris Enter the new value, or press ENTER for the default Login Shell [/bin/bash]: 

This is telling me my shell is /bin/bash and letting me change it.

Finally, echo $SHELL will do the same:

if it’s bash, why is it sometimes invoked explicitly like sudo bash -c «netstat -an | grep LISTEN | grep -v ^unix» ??

I think that’s a bad command personally — none of that even requires root. If it did, it would be better written as sudo netstat -an | grep LISTEN | grep -v ^unix . I see no reason to enclose that in a bash subshell.

@DrStrangeLove: If more than one command in the pipe needed root permissions, then sudo bash -c «. » would ensure that the entire pipe is executed by root.

If you don’t know which commands require root (and why), you shouldn’t run them. Otherwise, why trust any command/program/script any more than is required. If you can’t commandA | sudo commandB | commandC , you could do a sudo id first (runs the id command as root , but also acquires a «use sudo without password prompt» token that lasts for (default) 15 minutes.

Читайте также:  Boot to recovery mode linux

Источник

4 Ways to Check Which Shell You are Using on Linux

check user shell linux

Out of the box, Linux provides a wide variety of shells. There is bash (Bourne Again shell) shell which ships by default in many Linux distributions. We also have sh (Bourne Shell), tcsh (TC shell), csh (C shell), Zsh (Z shell) and ksh (Korn Shell).

Curious to know which shell you are using on your Linux system? In this guide, we explore different ways that you can use to check which shell you are currently using in Linux.

1. Using echo command

The Linux echo command is a built-in command that is used to print the output of a string which is passed as an argument. Additionally, you can use the echo command to check the shell that you are running commands in. To accomplish this, execute:

Output of echo $SHELL

The output shows that I am using the bash shell. Additionally, you can simply run the command:

Output of echo

To get the PID of the shell that you are currently in, run:

Output of echo $

2. Using ps command

Commonly used for listing running processes, the ps command in its basic format sheds light on the shell that you are using. Simply execute the command:

output of ps command

From the first line of the output, we can clearly see the PID of the shell and the last column prints out the type of shell, in this case — bash.

Alternatively, you can run the command:

Output of ps -p $

You can also use ps -p $$ -o args= which output just the shell name.

3. By viewing /etc/passwd file

The grep command can be used to probe the /etc/passwd file that contains attributes of the users such as username, user ID and group ID.

To display the shell used, invoke the command:

Using /etc/passwd file to show shell

At the very last segment, we get to see the bash used, in this case /bin/bash This also gives you a glimpse of which shell is opened first when you first log in to your system.

4. Using lsof command

Ordinarily, the lsof command, short for list of open files, is used to provide a list of open files on your system. However, when used with the -p $$ flag, it gives a pointer to the shell you are in when you look at the first column of the output.

For example, we can clearly see that we are on the bash shell.

Output of lsof -p $

How to check the valid login shells

We have seen various ways that you can employ to check the shell that you are currently in. If you want to know the valid shells on your system, check the /etc/shells file, This file will provide you with the full pathnames of valid login shells in your system. Using the cat command, view the file as shown:

Valid login shells

Conclusion

In this guide, we have shared simple but nifty ways that you can use to know which shell you are working on. This is important when writing scripts so that you can know how to start off writing the shebang header. We do hope that this tutorial was beneficial. Send us a shout and don’t forget to share this guide on your social platforms.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector