Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14486

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

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Stephane Chazelas <stephane.chazelas@gmail.com>
Newsgroups gnu.bash.bug
Subject "sh -a" sets the POSIXLY_CORRECT *environment* variable
Date Tue, 14 Aug 2018 16:50:26 +0100
Lines 81
Approved bug-bash@gnu.org
Message-ID <mailman.5099.1534267510.1292.bug-bash@gnu.org> (permalink)
NNTP-Posting-Host 208.118.235.17
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace usenet.stanford.edu 1534267512 4554 208.118.235.17 (14 Aug 2018 17:25:12 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=CJi353yIZAn8m70GHM1dKf39FApInH82t4cxsS0027U=; b=MCA3cb1zwlZ9JXYrFYv8uSQHTiL+MpX6jV4eDC/fOAooKCjVTUZ+rUSiQ1JBrcbnyV i97DjHpUd7EIH7UEb0iaOup2zyR9z+cvHNABHeqcH1csS2sEuMgjEyj7J050la7m0lgk Ib0HmDNeiE1Lv0MIZJQhNPVRqNAOrQDjrXMnKCQOFKkPjV3mLhpMuuMAPlKK61vVQzU1 kw8zA37U0hhTjLmzAHyZ8d/rr6DEhD3/iF7XnemhLjs7dS6LfzRFr0K3mZ1wMuid6fc/ RJyUSjndCf9eLNR25Dnyak3oFBM5IoaXoW2y2woN6sKzMkobeebPAqvvfAq6z67OJz+C he8Q==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=CJi353yIZAn8m70GHM1dKf39FApInH82t4cxsS0027U=; b=JkN1n2MJQ7KYTHQ7+FZ1XRP+YtO+39ExYCP0Z7FoxcdTCNuUgUcivbtpFaesZtdg5V bR4hwSdrg2IltECQk7tU2SjGk9cnaKNwLLt6iqWyrPCWK/tge3wptybHlXgRhjzjUj0Y 6bgYkfU5IAKfbzaycxhN3cLGwYFnqRCK0V2526L9EEKl/a2hXXEkL8lCBgje+teJdmLR 6EBKr8ls/pOz2J2V5kns3ZxOylrnaU+uB/2+Li1GS7hyzAnGGxlJ85KEJG009tn5uOJ2 3XtHVM9vzkmBSPYAFxVHsSIiglgD7HOZtrEesKK40TUq54FRgdO6pdISaA0gjRSnVQKp ARWA==
X-Gm-Message-State AOUpUlFNJLavdNH9/ivsN9wgOd3Ue58fJu/fN8e5IFOqkBBERMAkLS9W KBwpqZGn8YMzPw7en2p1Inh0rpsPbTg=
X-Google-Smtp-Source AA+uWPxcLnJvmT1uVViVAYTyEUJ5+wRgT+8F2RrkHHPu3/eSNmsilfZmvP9YVwHEDgobIF02PxNgUw==
X-Received by 2002:a1c:3282:: with SMTP id y124-v6mr11327612wmy.11.1534261828156; Tue, 14 Aug 2018 08:50:28 -0700 (PDT)
Content-Disposition inline
User-Agent NeoMutt/20171215
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 2a00:1450:400c:c09::234
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.21
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash/>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:14486

Show key headers only | View raw


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)

What is documented is that when that variable is set, then bash
enters the POSIX mode.

Now, that variable is understood as "enter POSIX mode" for many
GNU utilities and several non-GNU utilities (like those using
the GNU getopt API) not just bash.

When bash enters the POSIX mode while the allexport option is
on,  like in:

- sh -a
- sh -o allexport
- bash -o posix -o allexport...
- env SHELLOPTS=posix,allexport bash
- set -o posix # within bash -a

Then that POSIXLY_CORRECT ends up in the environment of all
commands started by that bash thereafter, which means they start
behaving differently.

IMO, bash's posix mode should only affect bash and its builtin
commands, not the other commands started within bash.

That a #! /bin/sh -a script should make all commands within
behave POSIXly is unexpected and means a script will behave
differently if /bin/sh is bashed on bash or another shell.

Maybe bash should not set POSIXLY_CORRECT, or not export it when
it sets it by itself. If it sets it, the documentation should
reflect it.

Maybe more generally, I think with allexport, bash should not
export the variables that it sets itself. One uses that option
for the variables they set to be available in other scripts
called within. It doesn't make sense to export things like
BASH_VERSION, BASHPID, OPTIND.

Per POSIX, "the export attribute shall be set for each variable
to which an assignment is performed (see Variable Assignment)",
though it goes on about getopts and read even though those are
not listed under "Variable Assignment".

In
set -a
getopts o VAR
printenv VAR

Some shells export VAR, few export OPTIND.

few export VAR in

set -a
for VAR in 1; do printenv VAR; done

(even though here I'd say it could be desirable).

All export it in "read VAR".

In any case, I think the POSIX specification should be
clarified.

-- 
Stephane

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

"sh -a" sets the POSIXLY_CORRECT *environment* variable Stephane Chazelas <stephane.chazelas@gmail.com> - 2018-08-14 16:50 +0100

csiph-web