57

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:

echo $SHELL

Result:

/bin/bash
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sago
  • 717
  • 1
  • 5
  • 9
  • 1
    Notice that you could use some strange shell, even one which is not POSIX compliant (e.g. [fish](http://fishshell.com/) or [es](https://wryun.github.io/es-shell/)...). 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) – Basile Starynkevitch Jul 07 '16 at 07:32

4 Answers4

64

This will do it:

$SHELL --version

In my case, the output is:

zsh 5.0.2 (x86_64-pc-linux-gnu)
20

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 $BASH_VERSION

For tcsh:

echo $version

For zsh:

echo $ZSH_VERSION
echo $ZSH_PATCHLEVEL # shows more detailed information

For ksh:

echo $KSH_VERSION

For fish:

echo $version

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:

echo "${BASH_VERSINFO[@]}"
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • This is a better answer. –  Jan 29 '18 at 18:14
  • 1
    What about /bin/sh or /bin/dash? (On Debian, /bin/sh is symlinked to dash. – HankB Mar 01 '19 at 17:21
  • 4
    @HankB: `dash` doesn't seem to have a version command-line option or command.On Debian or Ubuntu, `dpkg -l dash` will tell you which version is installed, but not necessarily the version you're running. – Keith Thompson Mar 01 '19 at 19:38
  • `/bin/dash` on Debian Buster contains the string `6323dd0fe3ec5af388e4ea4217a1f0092961d2.debug` which looks like some sort of commit ID maybe. This corresponds to Dash version 0.5.10.2-5. – tripleee Dec 22 '20 at 12:11
  • @tripleee It looks like it could be, but the hex string is 2 characters shorter than a git commit id, and there's there's no such commit in https://git.kernel.org/pub/scm/utils/dash/dash.git (or any commit whose id contains `6323dd0fe3ec5af388e4ea4217a1f0092961d2`). Ubuntu 20.04 has `2a16ad1517b3d714e7b3bdb5470b2c82eb25ff.debug`, same story. – Keith Thompson Dec 22 '20 at 18:30
8

There is a case when your shell does not have a command line parameter to determine the version directly. This case is Bourne shell. For Bourne shell I would recommend to use a script: https://www.in-ulm.de/~mascheck/various/whatshell/whatshell.sh. The script is pretty small so that it is not a big trouble to review it and understand how it is working. I have tested this script inside different shells on Linux and Solaris and it always gave the shell version for me.

Some examples:

Ubuntu 18.04

$ sh -c './whatshell.sh'
ash (Busybox 1.x)
$ bash -c './whatshell.sh'
bash 4.4.19(1)-release

CentOS 4

$sh -c './whatshell.sh'
bash 3.00.15(1)-release

Solaris 10

~> sh -c './whatshell.sh'
ksh88 Version (..-)11/16/88i (posix octal base)
~> bash -c './whatshell.sh'
bash 4.1.7(3)-release
~> csh -c './whatshell.sh' 
SVR4 Bourne shell (SunOS 5 variant)

AIX 6.1

~> sh -c './whatshell.sh'
ksh88 Version (..-)11/16/88f
~> bash -c './whatshell.sh'
bash 4.2.0(1)-release

This is also answers for the question Bourne shell version which was marked as off topic.

Alexander Samoylov
  • 2,358
  • 2
  • 25
  • 28
1

Just use command

echo $BASH_VERSION

It must give you the version of shell. BASH_VERSION is the environment variable which contains version of shell.

gaurav cmd
  • 21
  • 1