Re: sometimes i go huh (grep result)
Greg Wooledge wrote: > On Mon, Aug 27, 2018 at 10:36:12AM -0400, songbird wrote: >> me@ant(25)$ env | grep -F "-g" >> grep: invalid option -- 'g' > > You want either -- or -e. > > grep -F -- -g > grep -F -e -g i just found it interesting that after this many years of linux/unix i'd not remembered this issue. my golden years are ahead a bit yet so i sure hope this isn't a mental issue! lol songbird
Re: sometimes i go huh (grep result)
What's tripping you up is that some processing is being done by the shell before grep ever sees your pattern. Taking that into account, what grep is seeing is: songbird writes: > me@ant(25)$ env | grep -F "-g" grep -F -g > grep: invalid option -- 'g' > Usage: grep [OPTION]... PATTERN [FILE]... > Try 'grep --help' for more information. > me@ant(26)$ env | grep -F '-g' grep -F -g > grep: invalid option -- 'g' > Usage: grep [OPTION]... PATTERN [FILE]... > Try 'grep --help' for more information. > me@ant(27)$ env | grep -F 'CFL' > CFLAGS=-g grep -F CFL > me@ant(28)$ env | grep '\-g' > CFLAGS=-g grep \-g (I'll note that in this case you need the quotes or the shell would have stripped the \ . I'm guessing this one is doing what you want) > me@ant(29)$ env | grep '-g' grep -g > grep: invalid option -- 'g' > Usage: grep [OPTION]... PATTERN [FILE]... > Try 'grep --help' for more information. > me@ant(30)$ env | grep "-g" grep -g > grep: invalid option -- 'g' > Usage: grep [OPTION]... PATTERN [FILE]... > Try 'grep --help' for more information. > > > songbird
Re: sometimes i go huh (grep result)
On Mon, Aug 27, 2018 at 11:26:12AM -0400, Greg Wooledge wrote: > On Mon, Aug 27, 2018 at 11:20:42AM -0400, Roberto C. Sánchez wrote: > > env |grep [-]g > > Fails if there is a file named -g in the current directory, as that > matches the unquoted glob and causes it to expand. Also fails if failglob > is turned on, whether the file exists or not (fails differently in the > two cases). > > Also fails if nullglob is turned on, but that is definitely not > recommended in interactive shells. > Quite right. In my haste I forgot the quotes: env |grep '[-]g' -- Roberto C. Sánchez
Re: sometimes i go huh (grep result)
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Mon, Aug 27, 2018 at 11:19:25AM -0400, Greg Wooledge wrote: > On Mon, Aug 27, 2018 at 10:36:12AM -0400, songbird wrote: > > me@ant(25)$ env | grep -F "-g" > > grep: invalid option -- 'g' > > You want either -- or -e. > > grep -F -- -g > grep -F -e -g More generally, '--' is convention for "end of option arguments, normal arguments from here on". Most utilities nowadays stick to that convention. It was introduced precisely for this case. Note that quoting, as you do (i.e. "-g") can't work, because the shell unwraps that level of quotes; grep will still see -g and think it's an option. This quoting will help to "protect" whitespace: grep foo bar will see two arguments, foo and bar, whereas grep "foo bar" will see one, "foo bar". Cheers - -- tomás -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAluEGYYACgkQBcgs9XrR2kb6vACfSGHIgX57p5r3oyJ+5vNFQgCX sVUAn13XlvIHlRGYmzNeLfEpDwaDDFQx =vMte -END PGP SIGNATURE-
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' > CFLAGS=-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. Regards, -- Nicolas George signature.asc Description: Digital signature
Re: sometimes i go huh (grep result)
On Mon, Aug 27, 2018 at 11:20:42AM -0400, Roberto C. Sánchez wrote: > env |grep [-]g Fails if there is a file named -g in the current directory, as that matches the unquoted glob and causes it to expand. Also fails if failglob is turned on, whether the file exists or not (fails differently in the two cases). Also fails if nullglob is turned on, but that is definitely not recommended in interactive shells.
Re: sometimes i go huh (grep result)
On Mon, Aug 27, 2018 at 10:36:12AM -0400, songbird wrote: > me@ant(25)$ env | grep -F "-g" > grep: invalid option -- 'g' > Usage: grep [OPTION]... PATTERN [FILE]... > Try 'grep --help' for more information. > me@ant(26)$ env | grep -F '-g' > grep: invalid option -- 'g' > Usage: grep [OPTION]... PATTERN [FILE]... > Try 'grep --help' for more information. > me@ant(27)$ env | grep -F 'CFL' > CFLAGS=-g > me@ant(28)$ env | grep '\-g' > CFLAGS=-g > me@ant(29)$ env | grep '-g' > grep: invalid option -- 'g' > Usage: grep [OPTION]... PATTERN [FILE]... > Try 'grep --help' for more information. > me@ant(30)$ env | grep "-g" > grep: invalid option -- 'g' > Usage: grep [OPTION]... PATTERN [FILE]... > Try 'grep --help' for more information. > > try this: env |grep [-]g Regards, -Roberto -- Roberto C. Sánchez
Re: sometimes i go huh (grep result)
On Mon, Aug 27, 2018 at 10:36:12AM -0400, songbird wrote: > me@ant(25)$ env | grep -F "-g" > grep: invalid option -- 'g' You want either -- or -e. grep -F -- -g grep -F -e -g
sometimes i go huh (grep result)
me@ant(25)$ env | grep -F "-g" grep: invalid option -- 'g' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. me@ant(26)$ env | grep -F '-g' grep: invalid option -- 'g' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. me@ant(27)$ env | grep -F 'CFL' CFLAGS=-g me@ant(28)$ env | grep '\-g' CFLAGS=-g me@ant(29)$ env | grep '-g' grep: invalid option -- 'g' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. me@ant(30)$ env | grep "-g" grep: invalid option -- 'g' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. songbird