Search code examples
bashmacosgrepmv

Colored grep causing trouble when run in subprocess (OS X)


I'm having troubles in doing simple stuff with grep in my Mac. I'd like to rename a file, and for that I use grep and sed.

  • If I don't use grep, it works fine.

enter image description here

  • If I use grep, it doesn't work: mv shows a weird error.

enter image description here

For what I understand, the reason may be the colored grep I am using, and then mv uses not the filename, but the code for displaying the colored filename in the console (as elm content is a colored yum.txt, instead of a normal one).

Is this the reason? What can I do?


Solution

  • You are right, those strange characters are indeed color codes. You can search for the --color= option either in GREP_OPTIONS environment variable (printenv) and/or your user defined aliases.

    Then just change --color=always to --color=auto; with the --color=auto option set, grep should display color codes only when the standard output is connected to a terminal and plain text otherwise.