Here is a simple wrapper for grep for searching through a Git repository. For some reason, the colors are gone when grep is called though a bash script. How do I fix it?
I was using this command
grep $1 . -R --exclude-dir=.git --line-number
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
When you run
grep in a terminal it shows colours because of this default alias in your
alias grep='grep --color=auto'
~/.bashrc is only sourced by interactive shells. When you run a script, it runs in a non-interactive shell, so the alias is not available.
To preserve the colouring, add the
--color=auto flag to the
grep command inside the script:
grep $1 . -R --exclude-dir=.git --line-number --color=auto