Unfortunately, the current edition of POSIX sh standards does not offer either an official command line flag nor environment variable for reliably accessing the shell's version.
Fortunately, there are workarounds. One can try supplying --version
to the shell, for most bash derivatives (except dash and posh). Or echo "$BASH_VERSION"
(bash), echo "$ZSH_VERSION"
(zsh), echo "${.sh.version}"
(ksh).
Some packages differentiate versions in the binary filename, like how Python v3's command line application name is python3
. It's possible that the current process name of a shell includes the major version, or a shell's binary path like /bin/sh
links to a more specific path like /bin/bash4.4.12
. Unfortunately, this convention has not made its way into the tiny community of shell developers, so those checks are unlikely to yield useful results.
Yet, the packaging system may present a version number for the shell package in question. So run dpkg -l <shell>
(Debian derivatives), yum -l <shell>
(RHEL derivatives), emerge -Opv <shell>
(Gentoo), pacman -Qi dash
(Arch), brew list dash
(Homebrew), and so on. If the shell in question is sh
, then that shell is likely provided by the coreutils package, so query the package manager for coreutils
rather than sh
.
For RVM kin, cygwin, and other unix-like environments, the directory containing the shell may name the version of the shell. So grab the absolute path to the shell application and see if the name appears there.
Finally, the shell may simply be provided by the operating system, so uname -a; cat /etc/*release*
may provide at least some kind of identifier for tracking the shell version.
If all of these commands are joined with semicolons in a script, one would have a reasonably comprehensive brute force, nmap-like tool for identifying a given shell's version.
$SHELL
has absolutely nothing to do with the currently running shell. That's your default login shell and does not depend on what shell you happen to be using at the time. – terdon Sep 18 '17 at 16:15$SHELL
may not even be available... – Kusalananda Sep 18 '17 at 17:45#!/usr/bin/env sh
– rugk Sep 18 '17 at 21:00