Skip to content

env: -i does not empty the environment, and environment blocks are alphabetically re-sorted #1352

Closed

Description

@caomengxuan666

env: -i does not empty the environment, and environment blocks are alphabetically re-sorted

Two probe-verified divergences in env's environment-block handling (same-source harness under Wine, twice, GNU coreutils 9.4):

  1. env -i leaks the whole ambient environment. Probe: env -i Z=1 A=2 ./env (the child is env itself, printing the block it received). GNU child prints exactly:
    Z=1
    A=2
    
    The winx child prints a long block: A=2, then the session's ambient variables (SystemDrive, SystemRoot, and every other inherited variable present in the harness process), and Z=1 last. So winx's -i merged the assignments into the existing process environment instead of starting from an empty block — on a real Windows machine this means env -i passes the user's full environment to the child.
  2. Blocks are alphabetically sorted instead of insertion-ordered. In the same probe the winx block is ordered A=2 … Z=1 (alphabetical), while GNU preserves the order: inherited order, with command-line assignments appended in the order given. env.cpp builds the child block as a sorted map and also prints the no-command listing from that map, so plain env output order diverges from GNU's environment order too. Order is observable shell state (it is, for example, the iteration order of env output that scripts parse, and the search order some tools apply to duplicated keys).

Expected (GNU)

  • -i starts from an empty block; only the given assignments (plus the _= entry where applicable) reach the child.
  • No re-sorting: inherited order preserved, assignments appended in command-line order, for both the child block and the printed listing.

Regression seed

In-repo test named with this issue number: the env -i Z=1 A=2 ./env probe asserts the child's exact two lines in order; plain env output order asserted against a controlled two-variable parent block; duplicate-assignment override position asserted.

Activity

  1. added
    bug
    compat-gap
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugcompat-gap

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions