Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

So under Linux/AIX where ever.., when is it useful to use:

grep -- SOMEPATTERN

the Question is the "--" part. When is it usefull to use?

share|improve this question
add comment

2 Answers 2

up vote 16 down vote accepted

When "SOMEPATTERN" starts or may start (for instance if it's a variable like "$PATTERN" which you don't have full control on) with a - (dash) character.

Also with GNU grep (unless $POSIXLY_CORRECT is on), it's useful if other arguments (file names) may start with -.

Alternatively, you can do

grep -e -SOMEPATTERN- -- file1 file2 -xxx-

-- marks the end of options. It's useful everywhere where non-option arguments may start with a dash, and it doesn't harm, so it's a good habit to use it.

share|improve this answer
add comment

When the pattern starts with a dash, otherwise grep will think it is an option. Say, you are looking for "-a" in a text:

grep -a file.txt

grep will then try to find the pattern "file.txt" in the standard input, using the option -a. Therefore, you need to do

grep -- -a file.txt
share|improve this answer
3  
This does not only hold for grep. –  Bernhard Oct 11 '12 at 10:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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