Skip to content
Advertisement

similarity between the two files using grep, why this work: grep -i “-ffile1” file2

I have two files, file1 and file2, and want to find the similarity between the two files using grep.

why -f should flow by the file name without spaces when I surround the -f file1 with quotes?

this will work

grep -i "-ffile1" file2

but this will not work

grep -i "-f file1" file2

but if I remove the quotes these two cases will work

grep -i -ffile1 file2
grep -i -f file1 file2

Advertisement

Answer

By convention, one-letter options with arguments can be either presented as two arguments, i.e. either

(1)

  • Nth parameter to grep : -f
  • (N+1)st parameter to grep : the filename.

or

(2)

as a single parameter, where the name immedately follows the option letter: -ffilename

In your second attempt, "-f xxxx", you are passing a single parameter (which corresponds to case (2)), but what follows immediately after the option letter, is a space. Hence you specify a file name starting with a space. Such a file does not exist. Therefore, you see a different behaviour in this case.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement