With reference of this Q&A on AU.

Why behavior of GNU grep using -Pz parameters changed and doesn't support start of line ^ and $ end of line anchors?

Is this a bug or correct behavior?

Tested on Ubuntu 16.04 with kernel version 4.4.0-21-generic.

$ echo ^ | grep -Pz ^
grep: unescaped ^ or $ not supported with -Pz

1 Answer 1


This is desired behavior of GNU grep version 2.24 (released on March 10 2016) and above, and that's the fix for the bug which was introduced in GNU grep 2.5.

Looking into the source code:

if (*p == '$' || (*p == '^' && !after_unescaped_left_bracket))
  die (EXIT_TROUBLE, 0,
       _("unescaped ^ or $ not supported with -Pz"));

This change was made on Feb 21 2016, see this bug report for more details about this change.

Though that's GNU grep choice, it's a bug, as GNU grep compiles the PCRE regex with PCRE_MULTILINE set, and also reverted to calling pcre_exec for more than one record at a time, which is source of problem, as pointed out by Stéphane Chazelas

  • 3
    Shame, sounds like the wrong (non-)fix to the wrong problem. grep is calling pcre_compile with PCRE_MULTILINE which was the source of the problem in the first place AFAICT (as already noted at debbugs.gnu.org/cgi/bugreport.cgi?bug=16871#8) Nov 18, 2016 at 12:28
  • @StéphaneChazelas Good point, the source confirm that behavior. Thanks.
    – cuonglm
    Nov 18, 2016 at 12:34
  • 2
  • 2
    @KasiyA, it's now fixed properly in the latest development version (git head). Cuong, it's not as simple as PCRE_MULTILINE being used, it's also that GNU grep had reverted to calling pcre_exec for more than one record at a time. See the discussion at the bug for more details. My bad for missing the latest parts of the story. Nov 20, 2016 at 9:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .