Last Updated: February 25, 2016
· eviweb

How to retrieve and store java version in a bash variable

Sometimes, checking installed java version is needed in a shell script, and it is useful to easily retrieve and store it in a variable.

Here is a tip to do this in one line :

JAVA_VERSION=`echo "$(java -version 2>&1)" | grep "java version" | awk '{ print substr($3, 2, length($3)-2); }'`