0

This is part of a larger script but I distilled the problem down to this:

cm_safe(){
   git push | while read line; do
    echo "cm safe push: $line"
   done;
}

I want to prepend git output, so it would look like:

cm safe push: Everything up-to-date

but instead I just get:

Everything up-to-date

Is git writing to the tty or something directly? I dunno what's going on.

  • I did git push --help and I don't see an option on where to send stdout/stderr – user393961 Feb 26 at 20:35
2

git push writes to stderr so you would have to redirect that to stdout in order for it to be sent over the pipeline:

cm_safe(){
   git push 2>&1 | while IFS= read -r line; do
     echo "cm safe push: $line"
   done
}

Alternatively you could do:

git push |& while IFS= read -r line; do

I recommend reading What are the shell's control and redirection operators? for more information.

| improve this answer | |
  • ahhh git by default writes to stderr, now that is messed up, thanks – user393961 Feb 26 at 20:46
  • 1
    @AlexChannelmeter it's entirely standard practice for tools to write their status messages to stderr. – Ed Morton Feb 26 at 20:57
2

As you already know now, git pushs output is going to stderr, not stdout. Beyond that, you should always use while IFS= read -r line to read lines of input using the shell unless you have a very specific reason to drop either the IFS= or the -r. It's like always quoting your shell variables - it's something you remove when you have to, not something you add when you have to.

FWIW I'd use:

cm_safe() { git push 2>&1 | awk '{print "cm safe push:", $0}'; }

or:

cm_safe() { git push 2>&1 | sed 's/^/cm safe push: /'; }

anyway though given that using a shell loop to process text is considered bad practice.

| improve this answer | |
0

This seemed to work, but I don't know why:

 git push &> /dev/stdout

did it force git to send it's stdout/stderr to the terminal stdout? I don't get it

| improve this answer | |
  • 2
    &> will redirect both stdout and stderr, and you are redirecting them both into stdout so it's essentially the same as 2>&1 – jesse_b Feb 26 at 20:43
  • yes but that's not the issue - the issue was git was writing stdio to something other than stdout/stderr – user393961 Feb 26 at 20:46
  • 2
    No the issue is that is writes to stderr. – jesse_b Feb 26 at 20:46
  • yes you're right, ty appreciate it – user393961 Feb 26 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy