[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "sh -a" sets the POSIXLY_CORRECT *environment* variable

From: Stephane Chazelas
Subject: Re: "sh -a" sets the POSIXLY_CORRECT *environment* variable
Date: Wed, 15 Aug 2018 20:23:25 +0100
User-agent: NeoMutt/20171215

2018-08-15 11:05:06 -0400, Chet Ramey:
> On 8/14/18 11:50 AM, Stephane Chazelas wrote:
> > Hi,
> > 
> > This is from
> > https://unix.stackexchange.com/questions/462333/why-does-a-in-bin-sh-a-affect-sed-and-set-a-doesnt
> > (original investigation by Mark Plotnick)
> > 
> > Though not documented, enabling the POSIX mode in bash whether
> > with
> > 
> > - bash -o posix
> > - sh
> > - env SHELLOPTS=posix bash
> > - set -o posix # within bash
> > 
> > causes it to set the value of the $POSIXLY_CORRECT shell
> > variable to "y" (if it was not already set)
> Yes. This behavior dates from early 1997. It was put in on request so users
> could get a posix environment from the shell, since GNU utilities
> understand the POSIXLY_CORRECT variable. I could improve the documentation
> there, but a 20-plus-year-old feature isn't going to change.

Maybe there was a misunderstanding.

It's fine that bash enters POSIX mode when $POSIXLY_CORRECT is
set. IOW, it's fine that bash enters POSIX mode when the users
request it.

The problem I'm trying to raise is about the reverse behaviour:
that bash takes upon itself to request POSIX mode of all other
utilities when it itself enters POSIX mode, that it sets
$POSIXLY_CORRECT when it enters POSIX mode.

The problem would show up mostly with

#! /bin/sh -a

scripts on systems where sh is bash, and where the script relies
on non-POSIX behaviours of some GNU utilities.

I can't see how that could be seen as a feature, I can't imagine
anyone wanting that. If one wants to get a POSIX environment on
a GNU system, they would do:


(and yes, it's good that bash does honour it) (and yes, it's not
a very good interface as that means it can break scripts called
within that environment and that rely on non-POSIX behaviour of
some utilities, but that's beside the point being made here).

If one wants a POSIX shell, they can use

#! /bin/sh -


#! /usr/bin/env bash
set -o posix

if they can't rely on /bin/sh being a POSIX shell.

But that should not affect the behaviour of all other utilities
called within the script.

Without "-a", it's OK as the $POSIXLY_CORRECT variable is not
exported (it's not very useful that bash sets it (especially
considering it's not documented), but at least it's harmless).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]