Redirect java -version to file or variable

前端 未结 1 978
一生所求
一生所求 2021-01-18 03:58

Maybe it is a silly question, but I\'m trying to redirect the exit of \"java -version\" command to a file or variable but it doesn\'t work.

Server = Linux CentOS 6<

相关标签:
1条回答
  • 2021-01-18 04:27

    java -version writes to stderr (fileno 2), not stdout (fileno 1). You can redirect stderr to a file:

    java -version 2> test.txt
    # cat test.txt 
    # java version "1.7.0_25"
    # OpenJDK Runtime Environment
    # [...]
    

    Or you can redirect stderr to stdout:

    java_check=$(java -version 2>&1)
    # echo "$java_check"
    # java version "1.7.0_25" OpenJDK Runtime Environment [...]
    
    0 讨论(0)
提交回复
热议问题