title: How to use POSIXLY_CORRECT in grep?
body: <p>There is a variable <a href="https://www.gnu.org/software/bash/manual/bashref.html#index-POSIXLY_005fCORRECT" rel="nofollow noreferrer">POSIXLY_CORRECT</a> in Bash</p>
  
  <blockquote>
    <p>POSIXLY_CORRECT</p>
    
    <p>If this variable is in the environment when Bash starts, the shell
    enters POSIX mode (see Bash POSIX Mode) before reading the startup
    files, as if the --posix invocation option had been supplied. If it is
    set while the shell is running, Bash enables POSIX mode, as if the
    command</p>
  
  <pre><code>set -o posix
  </code></pre>
    
    <p>had been executed.</p>
  </blockquote>
  
  <p>I was told that some options of <code>grep</code> are not POSIX and so I confirmed in <a href="http://pubs.opengroup.org/onlinepubs/009604499/utilities/grep.html" rel="nofollow noreferrer">The Open Group Base Specifications Issue 6 for <code>grep</code></a>. So I checked the <a href="https://www.gnu.org/software/grep/manual/grep.html" rel="nofollow noreferrer">GNU grep manual</a> and found:</p>
  
  <blockquote>
    <p><em>grep</em> comes with a rich set of options: some from POSIX and some being GNU extensions. Long option names are always a GNU extension,
    even for options that are from POSIX specifications. Options that are
    specified by POSIX, under their short names, are explicitly marked as
    such to facilitate POSIX-portable programming. A few option names are
    provided for compatibility with older or more exotic implementations.</p>
  </blockquote>
  
  <p>And it also mentions:</p>
  
  <blockquote>
    <p><a href="https://www.gnu.org/software/grep/manual/grep.html#Environment-Variables" rel="nofollow noreferrer">2.2 Environment Variables</a></p>
    
    <p>The behavior of grep is affected by the following environment variables.</p>
    
    <p><strong>POSIXLY_CORRECT</strong><br>
    If set, grep behaves as POSIX requires; otherwise, grep behaves more like other GNU programs. POSIX requires that options that follow file names must be treated as file names; by default, such options are permuted to the front of the operand list and are treated as options. Also, POSIXLY_CORRECT disables special handling of an invalid bracket expression. See invalid-bracket-expr.</p>
  </blockquote>
  
  <p>Using the part <em>Long option names are always a GNU extension, even for options that are from POSIX specifications</em> I said: let's try the variable POSIXLY_CORRECT against that.</p>
  
  <p>So I did try with something that is not POSIX:</p>
  
  <pre><code>$ echo "HELLO" | grep --ignore-case 'hello'
  HELLO
  </code></pre>
  
  <p>But to my surprise it also works setting it:</p>
  
  <pre><code>$ echo "HELLO" | POSIXLY_CORRECT=1 grep --ignore-case 'hello'
  HELLO
  </code></pre>
  
  <p>What am I doing wrong? Shouldn't a set POSIXLY_CORRECT make <code>grep</code> not recognize a long option name?</p>
  
  <p>The same occurs if using an option (for example <code>-C</code>) that is not POSIX:</p>
  
  <pre><code>$ POSIXLY_CORRECT=1 grep -C 2 '2' &lt;&lt;&lt; "1
  2
  3"
  1
  2
  3
  </code></pre>
  
  <p>As well as doing all the same running <code>set -o posix</code> before.</p>