Store $(java -version) into a bash variable
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
add a comment |
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
add a comment |
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
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
bash java
asked Dec 12 at 17:18
akuma8
1052
1052
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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)
1
I don't know why you have put the inner brackets roundjava -version
: they are unnecessary and invoke a further subshell needlessly.
– AFH
Dec 12 at 18:30
add a comment |
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
java -version
prints java version message into stderr
2>&1
redirects stderr to stdout
|
takes lefthand command output and use it as input for righthand command
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.
- The character that
This will output something like 1.8.0_191
.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
1
I don't know why you have put the inner brackets roundjava -version
: they are unnecessary and invoke a further subshell needlessly.
– AFH
Dec 12 at 18:30
add a comment |
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)
1
I don't know why you have put the inner brackets roundjava -version
: they are unnecessary and invoke a further subshell needlessly.
– AFH
Dec 12 at 18:30
add a comment |
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)
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)
edited Dec 12 at 22:30
Kamil Maciorowski
23.6k155074
23.6k155074
answered Dec 12 at 17:56
Romeo Ninov
1,6641914
1,6641914
1
I don't know why you have put the inner brackets roundjava -version
: they are unnecessary and invoke a further subshell needlessly.
– AFH
Dec 12 at 18:30
add a comment |
1
I don't know why you have put the inner brackets roundjava -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
add a comment |
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
java -version
prints java version message into stderr
2>&1
redirects stderr to stdout
|
takes lefthand command output and use it as input for righthand command
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.
- The character that
This will output something like 1.8.0_191
.
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
add a comment |
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
java -version
prints java version message into stderr
2>&1
redirects stderr to stdout
|
takes lefthand command output and use it as input for righthand command
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.
- The character that
This will output something like 1.8.0_191
.
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
add a comment |
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
java -version
prints java version message into stderr
2>&1
redirects stderr to stdout
|
takes lefthand command output and use it as input for righthand command
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.
- The character that
This will output something like 1.8.0_191
.
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
java -version
prints java version message into stderr
2>&1
redirects stderr to stdout
|
takes lefthand command output and use it as input for righthand command
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.
- The character that
This will output something like 1.8.0_191
.
edited Dec 12 at 22:34
Kamil Maciorowski
23.6k155074
23.6k155074
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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