Last Updated: February 25, 2016
·
4.033K
· 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 :

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