I am looking for lines that have an apostrophe in them and tried a few expressions that seemed identical to me; however, some worked and others didn't. Why did I get the following behavior:
egrep \' file # works as expected
egrep "\'" file # seems to return all lines
egrep "[\']" file # works as expected
egrep '\'' file # seems to be waiting for more input
I think you're over complicating it, if you're trying to match
'
quote it in"
:Single vs. double quotes have differences when it comes to escaping. Put
echo
in front to see what actually gets sent toegrep
:The last case is prompting for more input because you're still within a single-quoted expression: it's not an escaped quote (
\'
) in single quotes (since that isn't how you escape single quotes.) It's a backslash between single quotes, with a trailing opening quote.Incidentally, to escape a single quote in a single-quoted string, use a construction like this:
What this is actually doing is putting a naked literal quote(
\'
) between two single-quoted strings. These are then all implicitly concatenated together.I have no idea why
"\'"
matches all lines (but it indeed seems to.)