How do I display git output in bash and store one string from the output in a variable?
up vote
1
down vote
favorite
I am running git push command in bash which generates some errors.
RESPONSE=$(git push "$target" --all | grep "error:" || true)
generates an output on the screen but variable $RESPONSE is empty
If I change the command to do this:
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
command runs silently but actually captures needed error in $RESPONSE
echo $RESPONSE
error: failed to push some refs to
'ssh://git@git.test.test.com:7999/test/test-test.git'
I really need to run this git push command in a way that it would hold the error above in $RESPONSE yet generate the entire output on the screen.
Running
RESPONSE=$(git push "$target" --all 2>&1 | tee -a log | grep "error:" || true)
did not help, unless I am missing something.
linux bash git error-handling
add a comment |
up vote
1
down vote
favorite
I am running git push command in bash which generates some errors.
RESPONSE=$(git push "$target" --all | grep "error:" || true)
generates an output on the screen but variable $RESPONSE is empty
If I change the command to do this:
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
command runs silently but actually captures needed error in $RESPONSE
echo $RESPONSE
error: failed to push some refs to
'ssh://git@git.test.test.com:7999/test/test-test.git'
I really need to run this git push command in a way that it would hold the error above in $RESPONSE yet generate the entire output on the screen.
Running
RESPONSE=$(git push "$target" --all 2>&1 | tee -a log | grep "error:" || true)
did not help, unless I am missing something.
linux bash git error-handling
1
Possible duplicate of How do I write stderr to a file while using "tee" with a pipe?
– mkrieger1
Nov 11 at 17:18
But possibly there is a better way if you use the lower-level "plumbing" Git commands for this kind of scripting instead of the "porcelain" commands intended for human interaction.
– mkrieger1
Nov 11 at 17:19
See: How to set a variable to the outputgit push
command
– Cyrus
Nov 11 at 17:30
1
See also: stackoverflow.com/questions/12451278/…
– mkrieger1
Nov 11 at 17:40
Possible duplicate of Capture stdout to a variable but still display it in the console
– tink
Nov 11 at 18:35
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am running git push command in bash which generates some errors.
RESPONSE=$(git push "$target" --all | grep "error:" || true)
generates an output on the screen but variable $RESPONSE is empty
If I change the command to do this:
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
command runs silently but actually captures needed error in $RESPONSE
echo $RESPONSE
error: failed to push some refs to
'ssh://git@git.test.test.com:7999/test/test-test.git'
I really need to run this git push command in a way that it would hold the error above in $RESPONSE yet generate the entire output on the screen.
Running
RESPONSE=$(git push "$target" --all 2>&1 | tee -a log | grep "error:" || true)
did not help, unless I am missing something.
linux bash git error-handling
I am running git push command in bash which generates some errors.
RESPONSE=$(git push "$target" --all | grep "error:" || true)
generates an output on the screen but variable $RESPONSE is empty
If I change the command to do this:
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
command runs silently but actually captures needed error in $RESPONSE
echo $RESPONSE
error: failed to push some refs to
'ssh://git@git.test.test.com:7999/test/test-test.git'
I really need to run this git push command in a way that it would hold the error above in $RESPONSE yet generate the entire output on the screen.
Running
RESPONSE=$(git push "$target" --all 2>&1 | tee -a log | grep "error:" || true)
did not help, unless I am missing something.
linux bash git error-handling
linux bash git error-handling
edited Nov 11 at 17:36
asked Nov 11 at 17:15
Dzerlig
846
846
1
Possible duplicate of How do I write stderr to a file while using "tee" with a pipe?
– mkrieger1
Nov 11 at 17:18
But possibly there is a better way if you use the lower-level "plumbing" Git commands for this kind of scripting instead of the "porcelain" commands intended for human interaction.
– mkrieger1
Nov 11 at 17:19
See: How to set a variable to the outputgit push
command
– Cyrus
Nov 11 at 17:30
1
See also: stackoverflow.com/questions/12451278/…
– mkrieger1
Nov 11 at 17:40
Possible duplicate of Capture stdout to a variable but still display it in the console
– tink
Nov 11 at 18:35
add a comment |
1
Possible duplicate of How do I write stderr to a file while using "tee" with a pipe?
– mkrieger1
Nov 11 at 17:18
But possibly there is a better way if you use the lower-level "plumbing" Git commands for this kind of scripting instead of the "porcelain" commands intended for human interaction.
– mkrieger1
Nov 11 at 17:19
See: How to set a variable to the outputgit push
command
– Cyrus
Nov 11 at 17:30
1
See also: stackoverflow.com/questions/12451278/…
– mkrieger1
Nov 11 at 17:40
Possible duplicate of Capture stdout to a variable but still display it in the console
– tink
Nov 11 at 18:35
1
1
Possible duplicate of How do I write stderr to a file while using "tee" with a pipe?
– mkrieger1
Nov 11 at 17:18
Possible duplicate of How do I write stderr to a file while using "tee" with a pipe?
– mkrieger1
Nov 11 at 17:18
But possibly there is a better way if you use the lower-level "plumbing" Git commands for this kind of scripting instead of the "porcelain" commands intended for human interaction.
– mkrieger1
Nov 11 at 17:19
But possibly there is a better way if you use the lower-level "plumbing" Git commands for this kind of scripting instead of the "porcelain" commands intended for human interaction.
– mkrieger1
Nov 11 at 17:19
See: How to set a variable to the output
git push
command– Cyrus
Nov 11 at 17:30
See: How to set a variable to the output
git push
command– Cyrus
Nov 11 at 17:30
1
1
See also: stackoverflow.com/questions/12451278/…
– mkrieger1
Nov 11 at 17:40
See also: stackoverflow.com/questions/12451278/…
– mkrieger1
Nov 11 at 17:40
Possible duplicate of Capture stdout to a variable but still display it in the console
– tink
Nov 11 at 18:35
Possible duplicate of Capture stdout to a variable but still display it in the console
– tink
Nov 11 at 18:35
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
One solution is to use tee
; just not exactly the way you showed. Taking it step by step will perhaps make the solution easier to understand:
git push "$target" --all
will send the error you want to STDERR. That's why you added 2>&1
, to redirect STDERR to STDOUT.
git push "$target" --all 2>&1
Then your pipeline (grep, etc.) is able to pick it up and eventually the variable capture is able to see it when you do
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
But because the error is no longer going to STDERR, and STDOUT is now being captured instead of sent to the screen, the output disappears.
So what you want to use tee
for, is to put the output on both STDERR (for the screen) and STDOUT (for your pipeline and eventual variable capture).
RESPONSE=$(git push "$target" --all 2>&1 |tee >(cat 1>&2) | grep "error:" || true)
This will likely work as you intend, but be aware that everything you see on the screen - all output from the git
command, error or otherwise - is now being passed on STDERR.
There aren't many practical reasons why this would be better than the answer about capturing to the variable and then echoing the variable (per miimote's answer), but if for some reason the non-sequential command structure seems better to you, this is a way to do it.
Thank you so much! That worked! You represent of best of StackOverflow community where you do explain how everything works. It was exceptionally helpful. I wish I can give you more points. Much, much appreciated!
– Dzerlig
Nov 11 at 19:30
add a comment |
up vote
1
down vote
The first line
RESPONSE=$(git push "$target" --all | grep "error:" || true)
stores the response of the command in the var RESPONSE
. So it is done in bash for any construction like VAR=$(command)
. If exists an error the var is empty but generates an output for the user.
If you add 2>&1
, you are saying the same but if exists an error the output is the file $1
, in your case the var $RESPONSE
.
You could do this
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true); echo $RESPONSE
You can read more about command substitution and redirections
thanks for your response! I really need to be able to do both at the same time, just can't figure out the right command to display the output and store the error in $RESPONSE.
– Dzerlig
Nov 11 at 18:20
So your comment really just restates the question but doesn't really answer it, unless I am missing something
– Dzerlig
Nov 11 at 18:40
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
One solution is to use tee
; just not exactly the way you showed. Taking it step by step will perhaps make the solution easier to understand:
git push "$target" --all
will send the error you want to STDERR. That's why you added 2>&1
, to redirect STDERR to STDOUT.
git push "$target" --all 2>&1
Then your pipeline (grep, etc.) is able to pick it up and eventually the variable capture is able to see it when you do
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
But because the error is no longer going to STDERR, and STDOUT is now being captured instead of sent to the screen, the output disappears.
So what you want to use tee
for, is to put the output on both STDERR (for the screen) and STDOUT (for your pipeline and eventual variable capture).
RESPONSE=$(git push "$target" --all 2>&1 |tee >(cat 1>&2) | grep "error:" || true)
This will likely work as you intend, but be aware that everything you see on the screen - all output from the git
command, error or otherwise - is now being passed on STDERR.
There aren't many practical reasons why this would be better than the answer about capturing to the variable and then echoing the variable (per miimote's answer), but if for some reason the non-sequential command structure seems better to you, this is a way to do it.
Thank you so much! That worked! You represent of best of StackOverflow community where you do explain how everything works. It was exceptionally helpful. I wish I can give you more points. Much, much appreciated!
– Dzerlig
Nov 11 at 19:30
add a comment |
up vote
2
down vote
accepted
One solution is to use tee
; just not exactly the way you showed. Taking it step by step will perhaps make the solution easier to understand:
git push "$target" --all
will send the error you want to STDERR. That's why you added 2>&1
, to redirect STDERR to STDOUT.
git push "$target" --all 2>&1
Then your pipeline (grep, etc.) is able to pick it up and eventually the variable capture is able to see it when you do
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
But because the error is no longer going to STDERR, and STDOUT is now being captured instead of sent to the screen, the output disappears.
So what you want to use tee
for, is to put the output on both STDERR (for the screen) and STDOUT (for your pipeline and eventual variable capture).
RESPONSE=$(git push "$target" --all 2>&1 |tee >(cat 1>&2) | grep "error:" || true)
This will likely work as you intend, but be aware that everything you see on the screen - all output from the git
command, error or otherwise - is now being passed on STDERR.
There aren't many practical reasons why this would be better than the answer about capturing to the variable and then echoing the variable (per miimote's answer), but if for some reason the non-sequential command structure seems better to you, this is a way to do it.
Thank you so much! That worked! You represent of best of StackOverflow community where you do explain how everything works. It was exceptionally helpful. I wish I can give you more points. Much, much appreciated!
– Dzerlig
Nov 11 at 19:30
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
One solution is to use tee
; just not exactly the way you showed. Taking it step by step will perhaps make the solution easier to understand:
git push "$target" --all
will send the error you want to STDERR. That's why you added 2>&1
, to redirect STDERR to STDOUT.
git push "$target" --all 2>&1
Then your pipeline (grep, etc.) is able to pick it up and eventually the variable capture is able to see it when you do
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
But because the error is no longer going to STDERR, and STDOUT is now being captured instead of sent to the screen, the output disappears.
So what you want to use tee
for, is to put the output on both STDERR (for the screen) and STDOUT (for your pipeline and eventual variable capture).
RESPONSE=$(git push "$target" --all 2>&1 |tee >(cat 1>&2) | grep "error:" || true)
This will likely work as you intend, but be aware that everything you see on the screen - all output from the git
command, error or otherwise - is now being passed on STDERR.
There aren't many practical reasons why this would be better than the answer about capturing to the variable and then echoing the variable (per miimote's answer), but if for some reason the non-sequential command structure seems better to you, this is a way to do it.
One solution is to use tee
; just not exactly the way you showed. Taking it step by step will perhaps make the solution easier to understand:
git push "$target" --all
will send the error you want to STDERR. That's why you added 2>&1
, to redirect STDERR to STDOUT.
git push "$target" --all 2>&1
Then your pipeline (grep, etc.) is able to pick it up and eventually the variable capture is able to see it when you do
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true)
But because the error is no longer going to STDERR, and STDOUT is now being captured instead of sent to the screen, the output disappears.
So what you want to use tee
for, is to put the output on both STDERR (for the screen) and STDOUT (for your pipeline and eventual variable capture).
RESPONSE=$(git push "$target" --all 2>&1 |tee >(cat 1>&2) | grep "error:" || true)
This will likely work as you intend, but be aware that everything you see on the screen - all output from the git
command, error or otherwise - is now being passed on STDERR.
There aren't many practical reasons why this would be better than the answer about capturing to the variable and then echoing the variable (per miimote's answer), but if for some reason the non-sequential command structure seems better to you, this is a way to do it.
answered Nov 11 at 18:47
Mark Adelsberger
19.4k11119
19.4k11119
Thank you so much! That worked! You represent of best of StackOverflow community where you do explain how everything works. It was exceptionally helpful. I wish I can give you more points. Much, much appreciated!
– Dzerlig
Nov 11 at 19:30
add a comment |
Thank you so much! That worked! You represent of best of StackOverflow community where you do explain how everything works. It was exceptionally helpful. I wish I can give you more points. Much, much appreciated!
– Dzerlig
Nov 11 at 19:30
Thank you so much! That worked! You represent of best of StackOverflow community where you do explain how everything works. It was exceptionally helpful. I wish I can give you more points. Much, much appreciated!
– Dzerlig
Nov 11 at 19:30
Thank you so much! That worked! You represent of best of StackOverflow community where you do explain how everything works. It was exceptionally helpful. I wish I can give you more points. Much, much appreciated!
– Dzerlig
Nov 11 at 19:30
add a comment |
up vote
1
down vote
The first line
RESPONSE=$(git push "$target" --all | grep "error:" || true)
stores the response of the command in the var RESPONSE
. So it is done in bash for any construction like VAR=$(command)
. If exists an error the var is empty but generates an output for the user.
If you add 2>&1
, you are saying the same but if exists an error the output is the file $1
, in your case the var $RESPONSE
.
You could do this
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true); echo $RESPONSE
You can read more about command substitution and redirections
thanks for your response! I really need to be able to do both at the same time, just can't figure out the right command to display the output and store the error in $RESPONSE.
– Dzerlig
Nov 11 at 18:20
So your comment really just restates the question but doesn't really answer it, unless I am missing something
– Dzerlig
Nov 11 at 18:40
add a comment |
up vote
1
down vote
The first line
RESPONSE=$(git push "$target" --all | grep "error:" || true)
stores the response of the command in the var RESPONSE
. So it is done in bash for any construction like VAR=$(command)
. If exists an error the var is empty but generates an output for the user.
If you add 2>&1
, you are saying the same but if exists an error the output is the file $1
, in your case the var $RESPONSE
.
You could do this
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true); echo $RESPONSE
You can read more about command substitution and redirections
thanks for your response! I really need to be able to do both at the same time, just can't figure out the right command to display the output and store the error in $RESPONSE.
– Dzerlig
Nov 11 at 18:20
So your comment really just restates the question but doesn't really answer it, unless I am missing something
– Dzerlig
Nov 11 at 18:40
add a comment |
up vote
1
down vote
up vote
1
down vote
The first line
RESPONSE=$(git push "$target" --all | grep "error:" || true)
stores the response of the command in the var RESPONSE
. So it is done in bash for any construction like VAR=$(command)
. If exists an error the var is empty but generates an output for the user.
If you add 2>&1
, you are saying the same but if exists an error the output is the file $1
, in your case the var $RESPONSE
.
You could do this
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true); echo $RESPONSE
You can read more about command substitution and redirections
The first line
RESPONSE=$(git push "$target" --all | grep "error:" || true)
stores the response of the command in the var RESPONSE
. So it is done in bash for any construction like VAR=$(command)
. If exists an error the var is empty but generates an output for the user.
If you add 2>&1
, you are saying the same but if exists an error the output is the file $1
, in your case the var $RESPONSE
.
You could do this
RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true); echo $RESPONSE
You can read more about command substitution and redirections
answered Nov 11 at 18:06
miimote
868
868
thanks for your response! I really need to be able to do both at the same time, just can't figure out the right command to display the output and store the error in $RESPONSE.
– Dzerlig
Nov 11 at 18:20
So your comment really just restates the question but doesn't really answer it, unless I am missing something
– Dzerlig
Nov 11 at 18:40
add a comment |
thanks for your response! I really need to be able to do both at the same time, just can't figure out the right command to display the output and store the error in $RESPONSE.
– Dzerlig
Nov 11 at 18:20
So your comment really just restates the question but doesn't really answer it, unless I am missing something
– Dzerlig
Nov 11 at 18:40
thanks for your response! I really need to be able to do both at the same time, just can't figure out the right command to display the output and store the error in $RESPONSE.
– Dzerlig
Nov 11 at 18:20
thanks for your response! I really need to be able to do both at the same time, just can't figure out the right command to display the output and store the error in $RESPONSE.
– Dzerlig
Nov 11 at 18:20
So your comment really just restates the question but doesn't really answer it, unless I am missing something
– Dzerlig
Nov 11 at 18:40
So your comment really just restates the question but doesn't really answer it, unless I am missing something
– Dzerlig
Nov 11 at 18:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53251195%2fhow-do-i-display-git-output-in-bash-and-store-one-string-from-the-output-in-a-va%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
1
Possible duplicate of How do I write stderr to a file while using "tee" with a pipe?
– mkrieger1
Nov 11 at 17:18
But possibly there is a better way if you use the lower-level "plumbing" Git commands for this kind of scripting instead of the "porcelain" commands intended for human interaction.
– mkrieger1
Nov 11 at 17:19
See: How to set a variable to the output
git push
command– Cyrus
Nov 11 at 17:30
1
See also: stackoverflow.com/questions/12451278/…
– mkrieger1
Nov 11 at 17:40
Possible duplicate of Capture stdout to a variable but still display it in the console
– tink
Nov 11 at 18:35