On OSX, using tr to squeeze git diff results translates spaces to tabs

My issue is very simple to replicate, and I’ve only tested it on OSX with the default version of “tr” that comes bundled with 10.8.3.

Whenever I do a:

  • Bash: how to implement locking in a multi process system?
  • GIT run script before git add
  • Using Bash to send space separated list of files to Git
  • Accidentally deleted many hidden files with “git clean -fd” and now I cannot install Ruby
  • Is there any risk or disadvantages of using: git config --global core.autocrlf false
  • How can you access an environment variable that has a space in its name in bash?
  • git diff --numstat <sha1> <sha2>

    it results in the following:

    1       1       test.php

    Notice how the separating character between the ones are all spaces. Now, when I pipe that command directly into a tr to squeeze those spaces out as follows:

    git diff --numstat <sha1> <sha2> | tr -s ' '

    It results in all the spaces being converted to one single tab character (I tried to paste it here but it didn’t actually work).

    The thing is, if I recall my bash correctly, this is not expected behavior at all. Also, when I tried to replicate this by putting this in a text file, cat-ing the text file and tr-ing it through the very same pipe, it does work as expected.

    Does anyone know why this is and how would you work around it gracefully?

    My end goal is to parse these results in python, which is significantly easier if there’s only one space separating the two numbers and the file name.

  • Can one git branch bloat another in the same remote?
  • Single git repo setup tracking multiple locations on hard drive
  • How to set the default author email for every git commit in vscode?
  • Automatic updating build number before commit to git
  • Checkout of git repository in pure PHP
  • Not visible GIT push
  • One Solution collect form web for “On OSX, using tr to squeeze git diff results translates spaces to tabs”

    I’m pretty sure those are not spaces in your output line. I can duplicate your problem when I emit a line containing tab characters. Try this modification:

    git diff --numstat <sha1> <sha2> | tr -s ' \t' '  '

    The first group is a space and tab character, the second group is two spaces.

    Git Baby is a git and github fan, let's start git clone.