I needed to know a command to find all of the versions of Java in the system path. I knew that which -a java
would print all places that the java executable can be found in the system path, but I then wanted to get the version info for each of these executables.
which -a java | xargs -I{} echo "echo {};{} -version;echo" | sh
This will print the path to each of the java executables found in the system path, as well as the version information of that executable, and separate entries with a newline. It works like so:
which -a java
into xargsxargs -I{} echo
to construct bash
commands, with each line in the input from which
replacing {}
bash
commands into the system's default shell executor. A sample output on my machine gives
/usr/bin/java
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
/usr/lib/jvm/java-8-oracle/bin/java
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
Note that if you want to know the versions of a different executable, you can modify the bash command that gets constructed to appropriately print version info. E.G. if you want to know all the versions of python
you can run
which -a python | xargs -I{} echo "echo {};{} --version;echo" | sh