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 –  Feb 26 '20 at 20:35

3 Answers3

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.

jesse_b
  • 37,005
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.

Ed Morton
  • 31,617
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