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?

2
  • It works as expected for me. Commented Aug 6, 2021 at 16:20
  • What do you see if you put javac -version in the script itself? Commented Aug 6, 2021 at 16:21

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

whoa.. didn't knew that, works now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.