- What shell am I in?
- re ps
- 8 Answers 8
- How can I find my shell version using a Linux command?
- 4 Answers 4
- 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 can I know which shell I am using?
What shell am I in?
Is there a command to identify the name/type of current shell, the path to the shell binary, and the version of the shell? I don’t need all of that, but the more I can get, the better. I want something that has the same feel of uname , pwd , whoami . Just a plain utility with a simple output. (which so far hasn’t showed up :/ )
re ps
Why -bash instead of the full path as it would be with everything else? What’s the deal with the dash there?
I’m starting to think the ‘test for features not for browsers’ thinking we use in javascript for the web may apply here.
I always like to counter a question with a question of my own: why do you need to know this—how do you intend to use that information and to what purpose?
8 Answers 8
The command or path to the currently running shell is stored in the environment variable $0 . To see its value, use:
This outputs either your currently running shell or the path to your currently running shell, depending on how it was invoked. Some processing might be required:
prompt:~$ echo $0 /bin/bash prompt:~$ sh sh-4.0$ echo $0 sh sh-4.0$ exit exit prompt:~$ /bin/sh sh-4.0$ echo $0 /bin/sh sh-4.0$
The $SHELL environment variable contains the user’s preferred shell, not necessarily the currently running shell.
So, $0 actually gives you the name of the currently running script. So, inside a script it’ll give you the name of the script, not the name of the shell that’s interpreting the script.
Well, technically, $0 gives you whatever the program executing your program decided to pass to the exec-family of syscalls. Try it on a login shell, for example, and you may see -bash — with the — in front.
If you don’t specify a program in the shebang line, I believe /bin/sh will be used. Unfortunately, I don’t believe there is a good portable way to determine what that shell is.
If you’re on e.g., Linux, you can find out the executable path through /proc :
and getting the executable name is easy through ps $$ .
But that won’t help you with the type of shell (except via a lookup table of known shells) nor with the version (AFAICT, there isn’t even a way to get the version from dash)
Hmm, you could use lsof -p $$ and look for the first txt entry, that seemed to do it on my Mac. But wow is that fragile.
So much for my quest for a simple solution. But it’s been fun so far. Using lsof was way far out there.
If no interpreter is specified, the currently running shell will be used rather than /bin/sh. (This is why people that use csh as their login shell and write scripts with no shabang belong in the 4th circle of hell.)
Try ($$ is shell variable set to process id of the shell):
or try this (/proc/self is aloso process id of the shell):
As regards to «-bash» — dash means it’s login shell. Type bash again and now you’ll see that the shell is just «bash» (without dash)
Yep, I agree these are more precise commands. This probably one of the «lazy» habits to use the same command for all tasks 🙂
@kch: Actually, -o comm according to POSIX. They had to abbreviate it for some reason. But it may have the same problems as $0 .
I think ‘finger’ is the right one you are looking for. Try this command:
Rather than trying to determine the shell currently being used, it is typically more appropriate to simply re-exec as the desired shell. This may be nothing more than an historical workaround of the fact that there is no reliable, portable way to determine the shell you are currently using. The best thing to do is to write your script to work in as many shells as possible so that it’s not an issue. (eg, portability matters, no matter how many people want to claim that «bash is everywhere»)
You can use the following in Arch Linux.
How can I find my shell version using a Linux command?
I would like to know about my shell version using a Linux command. I tried the following command, but it shows the type of the shell I am in. Command:
Notice that you could use some strange shell, even one which is not POSIX compliant (e.g. fish or es. ). You should know what shell you are using. If it is bash , indeed try bash —version . Or use your package management system ( dpkg -l bash on Debian or Ubuntu)
4 Answers 4
It depends on whether you want to know the version of your default login shell, or the version of the shell you’re currently running. They’re not necessarily the same.
For your default login shell, as the accepted answer says, $SHELL —version is likely to work. Most (but not all) shells accept a —version option. ( dash does not.) And this assumes that the value of $SHELL hasn’t been changed (there can be valid reasons to do so).
For the shell you’re currently running, if it happens to be bash you can type:
echo $ZSH_VERSION echo $ZSH_PATCHLEVEL # shows more detailed information
Again, this assumes that the relevant variable hasn’t been modified (there’s rarely any non-malicious reason to change it).
Bash in particular has an array variable $BASH_VERSINFO that gives more information in a form that’s easier to process programmatically. Printing $BASH_VERSINFO only prints the first element; to print all elements:
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.
If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks
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)