Discussion:
grep: invalid option -- _
(too old to reply)
TsanChung
2008-10-28 13:31:45 UTC
Permalink
How to fix the grep error?

$ grep tar *.txt
grep: invalid option -- _
Usage: grep [OPTION]... PATTERN [FILE]...

$ grep -V
grep -V
grep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne
"\007"

$ bash --version
GNU bash, version 3.1.17(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne
"\007"

$ uname -a
uname -a
Linux rose 2.6.18-1.2798.fc6 #1 SMP Mon Oct 16 14:54:20 EDT 2006 i686
i686 i386 GNU/Linux
echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne
"\007"
Clyde.bis
2008-10-28 13:51:35 UTC
Permalink
Post by TsanChung
How to fix the grep error?
$ grep tar *.txt
grep: invalid option -- _
Usage: grep [OPTION]... PATTERN [FILE]...
Try:

$ grep -- tar *.txt
Stephane CHAZELAS
2008-10-28 14:16:11 UTC
Permalink
Post by TsanChung
How to fix the grep error?
$ grep tar *.txt
grep: invalid option -- _
Usage: grep [OPTION]... PATTERN [FILE]...
$ grep -V
grep -V
grep (GNU grep) 2.5.1
[...]

It's a common problem with GNU tools that use GNU
getopt/getopt_long. Those allow options interleaved with
arguments even though that's prohibited by POSIX.

Here, you must have a filename that starts with "-", which is
taken as an option even though it appears after one or more
non-option argument(s) (tar at least).

Solutions:

POSIXLY_CORRECT=1 grep tar *.txt
(tells grep to behave POSIXly)

or:
grep -- tar *.txt
(-- explicitely marks the end of options)

or:
grep tar ./*.txt
(here all the remaining arguments will start with "./" so not
"-").
--
Stéphane
Loading...