Bash – POSIX-compatible/cross-shell way to get version of running shell

bashposixshellshell-scriptzsh

Is there a POSIX-compatible way or a way, which works on all shells, to get the version number of the shell, which is running?

With $SHELL ps -p$$ -ocmd= you can get the name/binary, so $SHELL --version $(ps -p$$ -ocmd=) --version may work, but in a very simple sh shell (tested in Travis-CI/Ubuntu Trusty) this does not work neither.

Is there a way or maybe at least a short/easy way, which works for most shells (e.g. in case you have to use case to differentiate between the shells)?

Edit: This is only for printing the version of the shell that is used, i.e. just for informative purposes. I do not want to do something with the return value, just show it to the user.

Best Answer

There is no --version flag defined in the POSIX standard for the sh utility. There is also no standard environment variable or shell variable that contain any kind of version information. This means that the shell doesn't even need make its version available at all.

If you need to test for a particular version of a shell, then your script is not likely to be POSIX conformant, so the question (as it stands) becomes irrelevant.

For the ksh shell, see How can I safely get the version of ksh?

Also related: How to test what shell I am using in a terminal?