0

I have a grep alias on my .bash_profile that display line numbers and color the matches. This is an example of my grep results:

$ grep test filename
1:This is a test (to be removed)
1:This is a second test (to be removed)

This is my alias:

alias grep="grep --color"

Is there any way of prevent the use of that alias when the grep command is executed on a script?

If I tried the following commands inside my script:

grep test filename | sed -E 's/^(.*) \(.*$/\1/g'
\grep test filename | sed -E 's/^(.*) \(.*$/\1/g'
command grep test filename | sed -E 's/^(.*) \(.*$/\1/g'
/usr/bin/grep test filename | sed -E 's/^(.*) \(.*$/\1/g'

All with the following results:

1:This is a test
1:This is a second test

Will be great to get the following result:

This is a test
This is a second test

I'm using grep (GNU grep) 2.21 and GNU bash, version 4.3.33.

closed as unclear what you're asking by Gilles, cuonglm, Braiam, Stephen Kitt, Anthon Jun 17 '15 at 4:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Try command grep ... instead of grep ... – Mark Plotnick Jun 16 '15 at 19:01
  • @MarkPlotnick I tried, and didn't work. – jherran Jun 16 '15 at 19:02
  • Aliases don't affect scripts unless you explicitly enable them. Did you use an alias or, or did you set GREP_OPTIONS? – muru Jun 16 '15 at 19:07
  • 2
    Then your script has shopt expand_aliases, which you should consider removing, if the script is under your control. From the manpage: Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt. – muru Jun 16 '15 at 19:12
  • 3
    Well: 1. Your alias as given doesn't enable line numbers. 2. The line number is repeated. I'm starting to think your file has hard-coded line numbers, or your output isn't representative. In either case, the alias is not at fault. – muru Jun 16 '15 at 19:19
3

Use \grep.

The (Bourne/POSIX) shell specification says that alias substitution in an interactive shell is suppressed when any character of the command word is quoted.

https://stackoverflow.com/a/16506263/91769

  • No way, didn't work for me. – jherran Jun 16 '15 at 19:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.