Which shell command in linux

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

Читайте также:  Режим реального времени 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).

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 .

Читайте также:  How can run exe in linux

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.

Читайте также:  Writing kernel module linux

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?

Источник

Which Command in Linux [Explained with Examples]

Linux which command is an extremely useful command for locating executable files located anywhere in the Linux system. Learn how to use it.

If you are wondering where exactly is a certain program is located, simply use which on it. The which command locates an executable file in your shell’s search path.

This Linux command has a simple syntax:

Let’s see how to use this simple but useful command.

Linux which command examples

Let’s say you want to know where is the Java executable, use which command:

The output could be like this:

Note that which only works on executable files. So you should use it only with the argument that you can run. For example, you install Java through the JDK package but you don’t run a command called ‘jdk’, you run ‘java’. So you use which command on java, not jdk.

If the which command doesn’t find the executable in the current path, it returns nothing.

Using which command with multiple executable files

You can provide more than one argument to which command:

which man java python nada
[email protected]:~$ which man java python nada /usr/bin/man /usr/bin/java /usr/bin/python

Did you notice something here? I gave it four arguments but the result is displayed for three of them only. It’s because ‘nada’ is not an executable. There is no output for that.

Display all pathnames with which command

The which command in Linux has only one option, -a. By default, which command prints only one pathname for its arguments.

If a program has executable in two places, say in /usr/bin/program and in /usr/local/bin/program, you can display both pathnames using the -a option.

Exit status of which command

If you use which command in a bash script, you may need to know its exit status.

Which command has the following exit status:

  • 0 – all arguments are found and executable
  • 1 – one or more arguments is nonexistent or non-executable
  • 2 – if an invalid option is specified

That’s all you need to know about which command in Linux. If you have questions or suggestions, do let me know in the comments below.

Источник

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