version=$(java -version 2>&1)
The version parameter accepts only one dash, and if you redirect stderr, that is, where the message is written, you will get the desired result.
As a side element, the use of two hyphens is an unofficial standard for Unix-like systems, but since Java tries to be almost identical on different platforms, it violates Unix / Linux expectations and behaves the same in this regard as it does on Windows, and as I suspect on Mac OS.
user unknown Mar 19 '12 at 10:09 2012-03-19 10:09
source share