Web lists-archives.com

Re: sometimes i go huh (grep result)

songbird (2018-08-27):
> me@ant(25)$ env | grep -F "-g"
> grep: invalid option -- 'g'

Maybe what you want is an explanation rather than just a solution.

Quotes are for the shell: they protect arguments that contain special
characters, so that commands get them as is.

For example, you need to write:

	echo "Fire*Wolf"

because without the quotes, the shell would try to find all the files in
the current directory with a name that matches the pattern.

Since the dash is not special for the shell, the quotes are unnecessary.
They do no harm, but have no consequences here:

	grep "-g"
	grep '-g'
	grep -g
	grep ""''""-"g"''

all invoke grep with one extra argument "-g".

The dash is special for programs that understand options (some do not;
some do with a different syntax, for example key=value), and need to be
escaped the way programs expect it. The usual escaping is that an
argument "--" means all following arguments are not options, even if
they start with a dash.

> me@ant(26)$ env | grep -F '-g'
> grep: invalid option -- 'g'

Same as above.

> me@ant(28)$ env | grep '\-g'

grep sees the argument starting with a backslash, it is not an option,
therefore it is the regexp. But backshash-dash could have had a special
semantic, like backslash-parentheses.

> me@ant(29)$ env | grep '-g'
> grep: invalid option -- 'g'

Same as above.

> me@ant(30)$ env | grep "-g"
> grep: invalid option -- 'g'

Same as above.


  Nicolas George

Attachment: signature.asc
Description: Digital signature