Issue
I answered a code-golf question here:
https://codegolf.stackexchange.com/q/89400/57100
My answer:
https://codegolf.stackexchange.com/a/89421/57100
I got a comment that had a sugestion to improve my answer, but I can't figure out exactly how it works.
The puzzle was to take any input and output just the characters that are in your source code.
The suggestion was: grep -o '[] [|\'\'greposrtu-]|sort -u
I get the -o
, sort -u
, the first ]
not closing the bracket expression, but I'm a little confused on the rest of the argument to grep. As far as I can tell, the first '
opens the string, but there isn't one to close it at the very end. Also, in the bracket expression the first \'
seems to close the string (if I take out both \'
s it waits for more input) but also matches \
Here are few other more minimal examples:
1. grep -o '[]']
matches ]
but not '
2. grep -o '[][\']
matches \
, [
, and ]
but not '
3. grep -o '[][\'\']
matches '
, [
, ]
, and \
4. grep -o '[']
gives an error: grep: Unmatched [ or [^
Questions about these examples:
1. Why does '
close the string, but ]
is after it and that closes the bracket expression in the string?
2. Why does \'
seem to match \
?
3. Why does a second \'
match '
this time instead of \
like it did the first time?
4. Why does '[]']
work, but '[']
doesn't?
Solution
In single quotes, \
has no special meaning. Therefore, the argument to grep
contains
[][|\
in single quotes\'
i.e. a single quotegrepostru-]
as literal characters,]
doesn't have a special meaning forgrep
if not being the 1st character inside of[...]
, and no special meaning forbash
.
Answered By - choroba