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:
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)
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 do I tell what type my shell is
How can I tell what type my shell is? ie, whether it’s traditional sh, bash, ksh, csh, zsh etc. Note that checking $SHELL or $0 won’t work because $SHELL isn’t set by all shells, so if you start in one shell and then start a different one you may still have the old $SHELL . $0 only tells you where the shell binary is, but doesn’t tell you whether /bin/sh is a real Bourne shell or bash. I presume that the answer will be «try some features and see what breaks», so if anyone can point me at a script that does that, that’d be great.
7 Answers 7
This is what I use in my .profile :
# .profile is sourced at login by sh and ksh. The zsh sources .zshrc and # bash sources .bashrc. To get the same behaviour from zsh and bash as well # I suggest "cd; ln -s .profile .zshrc; ln -s .profile .bashrc". # Determine what (Bourne compatible) shell we are running under. Put the result # in $PROFILE_SHELL (not $SHELL) so further code can depend on the shell type. if test -n "$ZSH_VERSION"; then PROFILE_SHELL=zsh elif test -n "$BASH_VERSION"; then PROFILE_SHELL=bash elif test -n "$KSH_VERSION"; then PROFILE_SHELL=ksh elif test -n "$FCEDIT"; then PROFILE_SHELL=ksh elif test -n "$PS3"; then PROFILE_SHELL=unknown else PROFILE_SHELL=sh fi
It does not make fine distinctions between ksh88, ksh95, pdksh or mksh etc., but in more than ten years it has proven to work for me as designed on all the systems I were at home on (BSD, SunOS, Solaris, Linux, Unicos, HP-UX, AIX, IRIX, MicroStation, Cygwin.)
I don’t see the need to check for csh in .profile , as csh sources other files at startup. Any script you write does not need to check for csh vs Bourne-heritage because you explicitly name the interpreter in the shebang line.