The reason that test="$(java -version)"
prints the result to the terminal directly is that java -version
outputs to standard error (stderr), not standard output (stdout).
Because there is no stdout output (which is what $(...)
captures), $test
is assigned an empty string.
The solution is to redirect standard error (stderr) to standard output (stdout).
version=$(java -version 2>&1)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…