I came across a script that has the line:
java_version=$($JAVA_HOME/bin/java -version 2>&1 | awk -F '"' '/version/ {print $2}' | cut -d '_' -f 1,3)
followed by the test:
if [[ -n "$java_version" ]]; then
...
The test when called from the command line succeeds. When called from Java application fails.
Some useful notes:
- The first line in the script is
#!/usr/bin/env bash. - The fact that the test succeeds when called from the command line means
java_versionis not empty. For further verification, Iecho'd right before the test the value ofjava_versioninto some external file and found the value there. - I changed the double brackets to singular and the test succeeded in both calling the script from the command line and the Java application.
- I'm running GNU/Linux Ubuntu 18.04, bash version 4.4.20, Java 8 (Oracle JDK).
Any ideas on what might cause this change of behavior?