-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.

Tim
  • 101,790

2 Answers2

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.

cuonglm
  • 153,898
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.