Store $(java -version) into a bash variable












0














Why when I do this: $ var=$(java -version) on my bash terminal, var is always empty? Same thing with $ java -version >> version.txt nothing is sent to the version.txt file.










share|improve this question



























    0














    Why when I do this: $ var=$(java -version) on my bash terminal, var is always empty? Same thing with $ java -version >> version.txt nothing is sent to the version.txt file.










    share|improve this question

























      0












      0








      0







      Why when I do this: $ var=$(java -version) on my bash terminal, var is always empty? Same thing with $ java -version >> version.txt nothing is sent to the version.txt file.










      share|improve this question













      Why when I do this: $ var=$(java -version) on my bash terminal, var is always empty? Same thing with $ java -version >> version.txt nothing is sent to the version.txt file.







      bash java






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 12 at 17:18









      akuma8

      1052




      1052






















          2 Answers
          2






          active

          oldest

          votes


















          3














          The command usually send such information to STDERR, not STDOUT. So in your case you should use commands like:



          java -version >>version.txt 2>&1


          and



          var=$((java -version) 2>&1)





          share|improve this answer



















          • 1




            I don't know why you have put the inner brackets round java -version: they are unnecessary and invoke a further subshell needlessly.
            – AFH
            Dec 12 at 18:30





















          3














          As Romeo has pointed, java -version writes in stderr, not in stdout, so you should use:



          var=$(java -version 2>&1)


          If you want to get the version only and not all the output of the java -version command, more convinient for scripting for example, you can use something like:



          var=$(java -version 2>&1 | awk -F '"' 'NR==1 {print $2}')


          Explanation





          1. java -version prints java version message into stderr


          2. 2>&1redirects stderr to stdout


          3. | takes lefthand command output and use it as input for righthand command


          4. awk -F '"' 'NR==1 {print $2}' is a bit more complicated, but basically it divides the input it receives into parts, allowing you to operate with each part separately (the most knowledgeable probably will throw me to the lions, it is a very bad summary of what is and what awk does).


            • The character that awk takes for the division is specified by the -F option, in this case it is divided by the character "

            • The last part specifies that only the second element ({print $2}) from the first line (NR==1) of the resulting division must be printed.




          This will output something like 1.8.0_191.






          share|improve this answer



















          • 1




            Thanks for the explanation. I am wondering why Java uses STDERR instead of STDOUT and how do you know this?
            – akuma8
            Dec 13 at 8:53











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1383051%2fstore-java-version-into-a-bash-variable%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          The command usually send such information to STDERR, not STDOUT. So in your case you should use commands like:



          java -version >>version.txt 2>&1


          and



          var=$((java -version) 2>&1)





          share|improve this answer



















          • 1




            I don't know why you have put the inner brackets round java -version: they are unnecessary and invoke a further subshell needlessly.
            – AFH
            Dec 12 at 18:30


















          3














          The command usually send such information to STDERR, not STDOUT. So in your case you should use commands like:



          java -version >>version.txt 2>&1


          and



          var=$((java -version) 2>&1)





          share|improve this answer



















          • 1




            I don't know why you have put the inner brackets round java -version: they are unnecessary and invoke a further subshell needlessly.
            – AFH
            Dec 12 at 18:30
















          3












          3








          3






          The command usually send such information to STDERR, not STDOUT. So in your case you should use commands like:



          java -version >>version.txt 2>&1


          and



          var=$((java -version) 2>&1)





          share|improve this answer














          The command usually send such information to STDERR, not STDOUT. So in your case you should use commands like:



          java -version >>version.txt 2>&1


          and



          var=$((java -version) 2>&1)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 12 at 22:30









          Kamil Maciorowski

          23.7k155074




          23.7k155074










          answered Dec 12 at 17:56









          Romeo Ninov

          1,6641914




          1,6641914








          • 1




            I don't know why you have put the inner brackets round java -version: they are unnecessary and invoke a further subshell needlessly.
            – AFH
            Dec 12 at 18:30
















          • 1




            I don't know why you have put the inner brackets round java -version: they are unnecessary and invoke a further subshell needlessly.
            – AFH
            Dec 12 at 18:30










          1




          1




          I don't know why you have put the inner brackets round java -version: they are unnecessary and invoke a further subshell needlessly.
          – AFH
          Dec 12 at 18:30






          I don't know why you have put the inner brackets round java -version: they are unnecessary and invoke a further subshell needlessly.
          – AFH
          Dec 12 at 18:30















          3














          As Romeo has pointed, java -version writes in stderr, not in stdout, so you should use:



          var=$(java -version 2>&1)


          If you want to get the version only and not all the output of the java -version command, more convinient for scripting for example, you can use something like:



          var=$(java -version 2>&1 | awk -F '"' 'NR==1 {print $2}')


          Explanation





          1. java -version prints java version message into stderr


          2. 2>&1redirects stderr to stdout


          3. | takes lefthand command output and use it as input for righthand command


          4. awk -F '"' 'NR==1 {print $2}' is a bit more complicated, but basically it divides the input it receives into parts, allowing you to operate with each part separately (the most knowledgeable probably will throw me to the lions, it is a very bad summary of what is and what awk does).


            • The character that awk takes for the division is specified by the -F option, in this case it is divided by the character "

            • The last part specifies that only the second element ({print $2}) from the first line (NR==1) of the resulting division must be printed.




          This will output something like 1.8.0_191.






          share|improve this answer



















          • 1




            Thanks for the explanation. I am wondering why Java uses STDERR instead of STDOUT and how do you know this?
            – akuma8
            Dec 13 at 8:53
















          3














          As Romeo has pointed, java -version writes in stderr, not in stdout, so you should use:



          var=$(java -version 2>&1)


          If you want to get the version only and not all the output of the java -version command, more convinient for scripting for example, you can use something like:



          var=$(java -version 2>&1 | awk -F '"' 'NR==1 {print $2}')


          Explanation





          1. java -version prints java version message into stderr


          2. 2>&1redirects stderr to stdout


          3. | takes lefthand command output and use it as input for righthand command


          4. awk -F '"' 'NR==1 {print $2}' is a bit more complicated, but basically it divides the input it receives into parts, allowing you to operate with each part separately (the most knowledgeable probably will throw me to the lions, it is a very bad summary of what is and what awk does).


            • The character that awk takes for the division is specified by the -F option, in this case it is divided by the character "

            • The last part specifies that only the second element ({print $2}) from the first line (NR==1) of the resulting division must be printed.




          This will output something like 1.8.0_191.






          share|improve this answer



















          • 1




            Thanks for the explanation. I am wondering why Java uses STDERR instead of STDOUT and how do you know this?
            – akuma8
            Dec 13 at 8:53














          3












          3








          3






          As Romeo has pointed, java -version writes in stderr, not in stdout, so you should use:



          var=$(java -version 2>&1)


          If you want to get the version only and not all the output of the java -version command, more convinient for scripting for example, you can use something like:



          var=$(java -version 2>&1 | awk -F '"' 'NR==1 {print $2}')


          Explanation





          1. java -version prints java version message into stderr


          2. 2>&1redirects stderr to stdout


          3. | takes lefthand command output and use it as input for righthand command


          4. awk -F '"' 'NR==1 {print $2}' is a bit more complicated, but basically it divides the input it receives into parts, allowing you to operate with each part separately (the most knowledgeable probably will throw me to the lions, it is a very bad summary of what is and what awk does).


            • The character that awk takes for the division is specified by the -F option, in this case it is divided by the character "

            • The last part specifies that only the second element ({print $2}) from the first line (NR==1) of the resulting division must be printed.




          This will output something like 1.8.0_191.






          share|improve this answer














          As Romeo has pointed, java -version writes in stderr, not in stdout, so you should use:



          var=$(java -version 2>&1)


          If you want to get the version only and not all the output of the java -version command, more convinient for scripting for example, you can use something like:



          var=$(java -version 2>&1 | awk -F '"' 'NR==1 {print $2}')


          Explanation





          1. java -version prints java version message into stderr


          2. 2>&1redirects stderr to stdout


          3. | takes lefthand command output and use it as input for righthand command


          4. awk -F '"' 'NR==1 {print $2}' is a bit more complicated, but basically it divides the input it receives into parts, allowing you to operate with each part separately (the most knowledgeable probably will throw me to the lions, it is a very bad summary of what is and what awk does).


            • The character that awk takes for the division is specified by the -F option, in this case it is divided by the character "

            • The last part specifies that only the second element ({print $2}) from the first line (NR==1) of the resulting division must be printed.




          This will output something like 1.8.0_191.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 12 at 22:34









          Kamil Maciorowski

          23.7k155074




          23.7k155074










          answered Dec 12 at 22:11









          Álvaro Orduna León

          312




          312








          • 1




            Thanks for the explanation. I am wondering why Java uses STDERR instead of STDOUT and how do you know this?
            – akuma8
            Dec 13 at 8:53














          • 1




            Thanks for the explanation. I am wondering why Java uses STDERR instead of STDOUT and how do you know this?
            – akuma8
            Dec 13 at 8:53








          1




          1




          Thanks for the explanation. I am wondering why Java uses STDERR instead of STDOUT and how do you know this?
          – akuma8
          Dec 13 at 8:53




          Thanks for the explanation. I am wondering why Java uses STDERR instead of STDOUT and how do you know this?
          – akuma8
          Dec 13 at 8:53


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1383051%2fstore-java-version-into-a-bash-variable%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

          Mangá

          Eduardo VII do Reino Unido