-2

From grep's manual

ps -ef | grep ’[c]ron’

If the pattern had been written without the square brackets, it would have matched not only the ps output line for cron, but also the ps output line for grep. Note that on some platforms, ps limits the output to the width of the screen; grep does not have any limit on the length of a line except the available memory

Doesn't [c]ron mean the same as cron, because [c] means a singleton set of alternative characters?

Why does it not match the ps output line for grep?

Thanks.

marked as duplicate by muru, Satō Katsura, Stephen Kitt, Archemar, user181255 Feb 16 '17 at 9:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

In term of regular expression, they're the same.

When you use bracket, the output of ps for grep line will be:

ps -ef | grep [c]ron

This text will be fed to grep standard in, grep tries to search for text cron which isn't contained in that line, so the line doesn't match.

Without bracket, the line contains text cron, so it matches.

0

In case of grep '[c]ron' the ps output would comprise a literal '[' 'c' ']' 'r' 'o' 'n' And hence the grep '[c]ron' would fail to match the grep command while still matching the cron job process running.

Whilst the case of grep 'cron' the ps output comprises a literal 'c' 'r' 'o' 'n' and hence the grep 'cron' would match itself, the grep process, and also match anyway the cron job process.

Not the answer you're looking for? Browse other questions tagged or ask your own question.