How do I display git output in bash and store one string from the output in a variable?









up vote
1
down vote

favorite
1












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.










share|improve this question



















  • 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














up vote
1
down vote

favorite
1












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.










share|improve this question



















  • 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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







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












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.






share|improve this answer




















  • 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

















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






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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',
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%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

























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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












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






share|improve this answer




















  • 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














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






share|improve this answer




















  • 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












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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線