0

I am using git grep to search for '\r' characters when i run it in the terminal it lists the output correctly but if i store it in a variable it lists out all the files it has searched for. Any idea on how to correct this.

My command is : git grep -I -c -r -l $'\r' *

I was trying something like this: OUTPUT=$(git grep -I -c -r -l $'\r' *)

I'm displaying output with: echo ${OUTPUT}

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Lovy
  • 45
  • 5
  • (1) Which operating system are you on? (important because if this is Windows, git adds CRs to all files by default). (2) What's the current value of git's `core.autocrlf` configuration flag? (3) Note that the above should be `echo "$OUTPUT"` -- if you don't use the quotes your output will get munged (string-split and glob-expanded) during the `echo` process, as described in [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Aug 10 '21 at 17:02
  • ...so if your `OUTPUT` variables contains a `*`, f/e, `echo $OUTPUT` will replace the `*` with a list of every file in your current directory, whereas `echo "$OUTPUT"` will show the actual `*` contained. – Charles Duffy Aug 10 '21 at 17:06
  • Also: Don't use all-caps names for your own variables; those names are used for variables meaningful to the shell itself, whereas variables with at least one lower-case character are reserved for application use and won't have unintended side effects on POSIX-compliant shells. (Thus, while you can't run `for PATH in */; do ...` without breaking your shell, `for path in */; do ...` works fine). zsh is not POSIX-compliant, and does not follow this rule, but this is one of many reasons not to use it. – Charles Duffy Aug 10 '21 at 17:06
  • I did whatever you said actually when storing in a variable it is giving all the files it has searched for '\r' rather than the file that contains the '\r' character . I don't know how is this happening. – Lovy Aug 10 '21 at 17:31
  • 1
    One easy explanation is that _all_ your files contain that character because git is checking out your files in crlf mode; another that you're on a Windows platform with a translation layer that's corrupting results by trying to hide that particular difference (making all files look native). Ideally, the question would have enough details to prove explanations like that wrong, or at least allow folks to test whether they're true for themselves. Right now, nobody but you can test. – Charles Duffy Aug 10 '21 at 17:36
  • I am actually on windows and using git bash. Also i tried the same thing with the grep command and facing the same issue – Lovy Aug 10 '21 at 17:48
  • _nod_, Windows is definitely the platform with the most to go wrong here, because it's normal and expected for text files to have CRLF endings on Windows, and a lot of tools try to play along and do what's locally normal/expected (and thus create those CRLFs) when they detect Windows as the active platform. – Charles Duffy Aug 10 '21 at 17:50
  • If you can't reproduce this issue in a real (not-a-translation-layer) UNIXy environment, I'd suggest revising the question to be _explicitly_ a question about msys2 (the translation layer used by the "git bash" suite) on Windows. – Charles Duffy Aug 10 '21 at 17:51
  • 1
    You wrote "I was trying something like this". Please reproduce as simple as possible your command, and show exact input/output. My result with `git grep...` is `error: unknown switch 'r'`. – Walter A Aug 10 '21 at 18:22

1 Answers1

0

If you need to further process the output, then saving to an environment var is not the best idea. The files will all be listed in a single line, and there is a limit on how long that line can be. Do something like this:

git grep -I -c -r -l $'\r' . | xargs -0 dos2unix

or save the result to a file and then read from that file:

git grep -I -c -r -l $'\r' . > /tmp/file.lst
for i in $(cat /tmp/file.list); do dos2unix "$i"; done

Notice also, that * is replaced with .. Recursive search starts with the current directory, no need for globbing.

xxor
  • 94
  • 3