On my Linux machine, it isn't clear to me why if I do the following then I don't get only the version string ("1.5.0_32").

# java -version | grep version | awk '{print $NF}'
java version "1.5.0_32"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_32-b05)
Java HotSpot(TM) Server VM (build 1.5.0_32-b05, mixed mode)

Why don't grep or awk work?

Just to show that grep and awk work on other example

# echo ' java version "1.5.0_32" ' | grep version  | awk '{print $NF}'

1 Answer 1


Try like this:

java -version 2>&1 | grep version  | awk '{print $NF}'

Looks like the output is going to stderr.

Also, grep is not needed:

java -version 2>&1 | awk '/version/{print $NF}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .