- Different Ways to Check Which Shell You are Using on Linux
- 4 Ways to Check Which Shell You are Using on Linux
- 1) Using echo command
- 2) Using ps command
- 3) By viewing /etc/passwd file
- 4) Using lsof command
- How to check the valid login shells
- Conclusion
- How do I find out what shell I am using on Linux/Unix?
- How can I find out what shell I am using?
- How do I check which shell am I using?
- How do I check how many shells are installed on my Linux box?
- Okay, so when I open the Terminal app, which shell is opened by default?
- How to check which shell am I using:
- Conclusion
- How to Find Which Shell You Are Using on Linux
- Find out which shell you are using in Linux
- Method 1
- Method 2
- Method 3
- Method 4
- Bonus Tip: Know the version of the shell
- How to test what shell I am using in a terminal?
- 11 Answers 11
Different Ways to Check Which Shell You are Using on Linux
A shell is a program through which users can interact with the Operating System. Linux provides commonly 4 types of Shell The Bourne Shell ( /bin/sh or /sbin/sh ), The C shell ( /bin/csh ), The Korn Shell ( /bin/ksh ), The GNU Bourne-Again Shell ( /bin/bash ). This article is about to Check that which shell we are using. Here, we discuss five ways by which we can check which shell we are using.
1) Using echo command: Basically, the echo command is used to print the input string, but it is also used to print the name of the shell which we are using with the help of the command.
2) Using ps command: ps command stands for “Process Status”. It is used to check the currently running status and their PIDs. If the ps command is run generally in the shell then it simply tells the name of the shell.
2. Shell name using ps
The first column tells the PID and the last column tells the type of shell i.e. bash.
3) By viewing /etc/passwd file: This tells the feature of the user just like it’s name, shell, and ID. This command is used with the grep command.
4) Using lsof command: lsof stands for LIST OF OPEN FILES. This command used when we want the list of open files in our system. By using the specific flag it gives a pointer to the shell and tells us about that which shell are we using and that specific flag is -p $$. The command looks like.
As you can see clearly in the 3rd line it tells us about the shell we are using.
5) Using readlink /proc/$$/exe: The readlink is used to print the file name. So with the help of this, we print the location of the current shell using the /proc/$$/exe. Command can be written as
Источник
4 Ways to Check Which Shell You are Using on 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:
The output shows that I am using the bash shell. Additionally, you can simply run the command:
To get the PID of the shell that you are currently in, run:
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:
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:
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:
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.
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:
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.
Источник
How do I find out what shell I am using on Linux/Unix?
B oth Linux and Unix provides various shell out of the box. One can find bash (Bourne Again shell), ksh (Korn shell), csh (C shell)/tcsh (TC shell), sh (Bourne shell) and more installed by default. However, how do you check which shell am I using? What is the best way to find out what shell I am using on Linux? The echo $SHELL is not so reliable. This page explains how to find out which shell I am using at a Linux, MacOS, FreeBSD, or Unix-like systems.
How can I find out what shell I am using?
The following echo command or printf command should work:
echo «$SHELL»
OR
printf «My current shell — %s\n» «$SHELL»
Please note that $SHELL is the shell for the current user but not necessarily the shell that is running at the moment. Try the following examples
How do I check which shell am I using?
Here is another old good Unix trick. Use the ps command with -p
ps -p $$
Sample outputs:
So what is a $ argument passed to the -p option? Remember $ returns the PID (process identification number) of the current process, and the current process is your shell. So running a ps on that number displays a process status listing of your shell. In that listing, you will find the name of your shell (look for CMD column).
ps -p $$
Sample outputs:
From my Linux box:
ps -p $$
Sample outputs:
You can store your shell name in a variable as follows :
MYSHELL=`ps -hp $$|awk ‘
Please note those are backquotes, not apostrophes. Or better try out the following if you have a bash shell:
MYSHELL=$(ps -hp $$|awk ‘
Another option is as follows:
echo $0
OR
printf «%s\n» $0
Sample outputs from the above commands:
Fig.01: Linux check which shell am I using
How do I check how many shells are installed on my Linux box?
The /etc/shells is a text file which contains the full pathnames of valid login shells. Type the following [nixmd name=”cat”] to see list how many shells are installed on your Linux or Unix box:
cat /etc/shells
Use /etc/shells file to check how many shells are installed on your system
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Okay, so when I open the Terminal app, which shell is opened by default?
Your default shell is defined in /etc/passwd file. So try the following grep command:
How to check which shell am I using:
Use the following Linux or Unix commands:
- ps -p $$ – Display your current shell name reliably.
- echo «$SHELL» – Print the shell for the current user but not necessarily the shell that is running at the movement.
- echo $0 – Another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems.
- readlink /proc/$$/exe – Another option to get the current shell name reliably on Linux operating systems.
- cat /etc/shells – List pathnames of valid login shells currently installed
- grep «^$USER» /etc/passwd – Print the default shell name. The default shell runs when you open a terminal window.
- chsh -s /bin/ksh – Change the shell used from /bin/bash (default) to /bin/ksh for your account
Conclusion
Sometimes things are not easy as they seem, and this page is the perfect example of it. I hope you found the suggestion useful when it comes to checking your current running shell. Bash users can display shell version by typing the following command:
$ bash —version
Here is what I got from my Ubuntu Linux 20.04 LTS desktop:
Источник
How to Find Which Shell You Are Using on 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:
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 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 process 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:
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 to test what shell I am using in a terminal?
How to check what shell I am using in a terminal? What is the shell I am using in MacOS?
11 Answers 11
Several ways, from most to least reliable (and most-to-least «heavy»):
- ps -p$$ -ocmd= . (On Solaris, this may need to be ps -p$$ -ofname= and on macOS and on BSD should be ps -p$$ -ocommand= .)
- Check for $BASH_VERSION , $ZSH_VERSION , and other shell-specific variables.
- Check $SHELL ; this is a last resort, as it specifies your default shell and not necessarily the current shell.
I’ve found that the following works in the four shells I have installed on my system (bash, dash, zsh, csh):
The following works on zsh, bash, and dash, but not on csh:
As the question asks for the shell used and does not talk about the potential arguments passed to it, here is a way that avoid showing them:
A note about some lighter implementations (Android phones, busybox, etc.): ps doesn’t always have support for the -p switch, but you can accomplish the search with a command like ps | grep «^$$ » . (This grep regex will uniquely identify the PID, so there will not be any false positives.)
There are two really simple ways:
- -h or finishing all options with = for not showing any header.
- -o comm for showing only the process basename ( bash instead of /bin/bash ).
- -p
list only process whith PID form list suplied.
Using the /proc process information pseudo-file system:
This option behaves exactly as the ps command above.
This /proc/PID/exe links to the file being executed, which in this case would point to /bin/bash, /bin/ksh, etc.
For getting only the name of the shell you can just use
This is the only option that will always give the same result even if you are in an script, sourced code, or terminal, as links to the binary of the shell interpreter in use.
Warning You must be aware that this will show the ultimate binary, so ksh may be linked to ksh93 or sh to bash.
The usage of /proc is really useful via the /proc/self , which links to the PID of the current command.
Источник