I have a simple script to search patterns in my code sources, named prgrep

grep -irnI --exclude-dir={.git,obj} --exclude=tags --color=auto "$@"

(The fact that it is a script and not an alias or function is that I want to be able to call it from inside vim and with any shell)

Note that the search is case insensitive (since I consider this a good default to search) and the script accepts any flags that grep accepts.

I would like grep to have a flag --no-ignore-case so that the caller of the script could override the -i flag of the script, but GNU grep does not provide this.

Do you have any simple idea to provide such functionality? Currently I have a separate script named Prgrep which performs case sensitive searches.

Recent versions of GNU grep do provide a --no-ignore-case option, which is exactly what I need. I'm using GNU grep 3.1, which still doesn't have this option.

  • How about an argument for the script? Inside the script you check $1 for any string you like and create a case statement with or without case-sensitive search. – eblock Jan 28 at 9:08
  • The desired behaviour is achieved simply by dropping -i - I don't understand what the question is. – Panki Jan 28 at 9:16
  • @eblock That would force a predefined number of arguments, and the script would have to handle all of them. I want to keep allowing all the grep options – Hobber Jan 28 at 9:20
  • 2
    I’m voting to close this question because the issue was fixed by updating grep. – Panki Jan 28 at 9:35
  • 1
    @Panki OK, but it would be nice to have a solution for old versions of grep... – Hobber Jan 28 at 9:36

New versions of grep have the option --no-ignore-case which overrides -i:

Do not ignore case distinctions in patterns and input data. This is the default. This option is useful for passing to shell scripts that already use -i, to cancel its effects because the two options override each other.

For older versions of grep, you could simply add this as option to your script:

if [ "$1" = "--no-ignore-case" ]; then
    grep -rnI --exclude-dir={.git,obj} --exclude=tags --color=auto "$@"
    grep -irnI --exclude-dir={.git,obj} --exclude=tags --color=auto "$@"

Note: --no-ignore-case will need to be the first argument when you call your script.

  • The only problem with this solution is that --no-ignore-case must always be the first argument in order to be matched – Hobber Jan 28 at 19:55
  • @Hobber short of re-doing the entire argument parsing of grep, there's no way to robustly handle that. Consider a command line like your_wrapper -hio --no-ignore-case ...: the --no-ignore-case should override not only the -i default from your wrapper, but also the -i from the command line, which means that it will have to parse and rebuild the command line of grep; have a look at the /usr/bin/zgrep wrapper script on Linux to have a taste of what that entails ;-) – Uncle Billy Jan 28 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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