1

For some reason this is not working on my machine. I have a test script test.sh containing

#! /usr/bin/env ksh
JV=$(javac -version)
echo "JV1: $JV eol"
echo "JV2: ${JV} eol"

Output

JV1:  eol
JV2:  eol

If I run command on terminal

>: javac -version

I get

>: javac 1.8.0_222

Environment:

OS: CentOS Linux release 7.7.1908

Bash: 4.2.46(2)


I had a look at this post, doesn't seem to work for me. Any suggestions?

4673_j
  • 477
  • 1
  • 6
  • 20

1 Answers1

3

java -version is writing to standard error, not standard output (which is what $(...) captures. You need to redirect the output.

JV=$(javac -version 2>&1)
chepner
  • 497,756
  • 71
  • 530
  • 681