Skip to content

fix: show only exported variables to the commands the shell runs - #285

Merged
ferdingler merged 5 commits into
strands-agents:mainfrom
ferdingler:fix/env-exported-only
Nov 20, 2026
Merged

ferdingler merged 5 commits into
strands-agents:mainfrom
ferdingler:fix/env-exported-only

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Every variable you set in the shell was visible to the commands it runs, exported or not. So x=1; env listed x=1 where bash lists nothing, and the same leak reached sh -c, scripts, lua's os.getenv, and commands handed to the embedder's command handler. export -n also did nothing.

Now the shell keeps track of which variables are exported, and the commands it runs only get those, plus any x=1 cmd assignments for that command. export, export -n, unset, readonly and function locals work the way they do in bash. Variables from the embedder or the starting environment (HOME, PATH, PWD, USER, anything set through the builder or set_env) stay exported, and cd exports PWD and OLDPWD like bash does. Subshells and command substitution still see everything, same as bash.

I compared a few dozen cases against bash 5.2 and they match, except for the existing gaps listed below. I added a differential case recorded from bash, plus tests for lua, embedder variables and the command handler. The full Rust suite passes. I didn't run the Python or Node suites locally because nothing in the bindings changed.

Two things I left out on purpose because they need their own fixes: x=1 f (when f is a function) and x=1 lua … on their own don't apply the assignment at all. That was already broken before this change. Also, sh -c still gets the parent's functions and readonly flags, which bash doesn't pass on.

Related Issues

Part of #207

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler
ferdingler requested a review from a team as a code owner November 13, 2026 05:17
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent mentioned this pull request Nov 13, 2026
9 of 10 tasks

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve. No blockers. @ferdingler, the export tracking holds up. I ran 145 bash-differential cases and found 0 regressions; 39 cases that diverged from bash on main now match it. The full suite passes on c16dde0.

One 🟡 inline: keep_exported_only copies the whole env before filtering it, so every command the shell runs pays for every large unexported variable. A one-line change fixes it.

Process::set_exported is new public API (os is a pub mod) and the PR has no needs-api-review label. It's small, but someone should apply the label or decide it isn't needed.

✅ What I verified
  • Commit: c16dde0 against main 5f80040.
  • Tests: cargo test --workspace --all-targets reported 2577 passed and 0 failed across 16 test binaries. The new unexported_hidden_from_env, lua_sees_only_exported and the_handler_sees_only_exported_variables tests pass, and they would fail on main.
  • Differential: 145 cases comparing the PR, main and bash 5.2. Results: 65 already matched bash, 39 are now fixed, 0 regressed, 30 are pre-existing gaps, and 11 changed but not to bash's output (see the appendix). The cases cover export, export -n/--, unset, readonly, locals including nested and recursive functions, prefix assignments in pipelines, sh -c, scripts, source, eval, ( ), $( ), background jobs, traps, xargs, find -exec, lua, cd, PWD/OLDPWD/PATH/HOME, getopts and read.
  • Code paths checked: I looked for other ways an env reaches a program. The command fallback (command_fallback.rs:147) reads the child that is already filtered at exec.rs:3929. xargs and find -exec re-enter run_pipeline. MCP and lua's internal Process::new calls build from env that is already filtered. The Python and JS bindings don't expose CommandContext.
  • Perf: with one 8 MB unexported variable, 2000 × basename takes 614 ms on the PR and 69 ms on main. With the suggestion below it takes 75 ms, and the differential classification is unchanged.
Questions (non-blocking)
  • Rust CommandFallback handlers no longer see plain X=1 variables. That is the point of the fix, but could it go in the release notes as a behaviour change for embedders?
Appendix: non-blocking (7)
  • ⚪ os.rs:835: the set_env doc still says "Set an environment variable". It now sets an unexported one, so a custom Kernel::new_process that builds Process::new(cwd, HashMap::new()) and then calls set_env hides those variables. Nothing in the tree does this. Suggested doc: "Set a shell variable (unexported unless already exported; see set_exported)".
  • ⚪ os.rs: set_exported is public but there's no is_exported, so embedders can set the flag and can't read it back.
  • ⚪ Child shells don't rebuild PWD at startup. After export -n PWD, sh -c 'echo $PWD' prints [], while bash's child prints its cwd. export -n PATH behaves the same way, but lookup still works. Both cases are contrived.
  • ⚪ After export x=0, running f(){ local x=1; export -n x; env; } prints nothing on the PR, x=1 on main and x=0 in bash. This is a bash quirk and the PR is closer to bash than main.
  • ⚪ export.rs:41: unknown flags (-p, -x, -nx) are treated as names. export -p prints nothing, as before, and now also adds "-p" to the exported set, which is harmless. Bash rejects these flags with exit 2.
  • Pre-existing, known to the author: prefix assignments are only applied on the pipeline path (exec.rs:3747). The single-command fast paths for builtins and functions (exec.rs:3526, exec.rs:3671) drop them, which is why X=1 f, X=1 lua … and X=1 xargs env lose X. I didn't file an issue because #207 already tracks this.
  • Pre-existing: export/export -p with no arguments prints nothing, bash's set -a isn't supported, and command/exec aren't available.
Reading order for human reviewers

src/os.rs (the exported set, keep_exported_only, local-scope restore), then src/exec.rs (the three call sites), then src/builtins/export.rs, then the rest.

Evidence: strands-agents/shell/pr/285/{adv-repro.sh,adv-repro.out,cargo-test.log,perf-repro.sh} in the Strandly artifacts bucket.

Comment thread src/os.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. How I handled the rest:

  • Cloning the env (🟡): fixed in ae07622, as suggested.
  • set_env doc: updated. It now says the variable stays unexported unless it already is, and points to set_exported.
  • set_exported is public with no is_exported: I made it pub to match mark_readonly, so code that builds a Process itself can export variables it adds after new. I took out is_exported because nothing used it. I didn't want to add a getter only for symmetry. A maintainer can decide whether this needs the needs-api-review label. I can't apply labels on this repo.
  • Unknown export flags (-p, -x): they were silently ignored before this PR and still are. A stray name in the exported set never shows up, because a variable named -p can't be set and child environments drop names that aren't set. Supporting export -p and rejecting bad flags is a separate change.
  • PWD in child shells after export -n PWD, and the local + export -n quirk: contrived, and the PR is already closer to bash than main was. Leaving them.
  • Release note for embedders: yes, command fallback handlers now see only exported variables. The PR description says so, and the CommandContext::env doc is updated.
  • Prefix assignments on single builtins and functions: pre-existing. Left for its own fix, as the description says.

@ferdingler ferdingler mentioned this pull request Nov 13, 2026
10 tasks done
ferdingler and others added 3 commits November 14, 2026 11:20
strands-agents#305 added this differential case with a known-difference marker noting
that every variable reached child commands, "fixed by strands-agents#285". This PR is
that fix: env and other child commands now see only exported variables,
so the case matches bash and the harness fails until the marker goes.
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up on fe1fc41: approve, no blockers.

Status of my earlier 🟡 (env copied for every command): fixed in ae07622 for unexported variables. With an 8 MB unexported variable, 2000 commands take 69 ms against 68 ms on main.

The fix doesn't cover a large exported variable, and neither did my suggestion. The same loop with an 8 MB exported variable takes 606 ms against 67 ms on main, and 124 ms against 53 ms at 1 MB. That's under 0.04 ms per command per MB, so I'd leave it; details are in the appendix.

The two merges with main are clean. I re-ran everything on the new head: 0 regressions in a 112-case bash differential, and the full suite passes (2663 tests).

Process::set_exported still has no API-review label. The author can't apply labels, so that's a maintainer's call.

✅ What I verified
  • Commits: head fe1fc41 against main 9851f51, which is also the merge base.
  • Delta since c16dde0: ae07622 (the clone fix and the set_env doc), two conflict-free merges bringing in 13 commits from main, and fe1fc41. The last one removes the known-difference marker #305 put on export_hidden_from_children.sh; the harness fails when a marked case matches bash, so the removal is required.
  • Tests: cargo test --workspace --all-targets exits 0 with 2663 passed, 0 failed and 0 ignored across 20 test binaries. That includes differential and the PR's own unexported_hidden_from_env, prefix_assignment_visible_to_env, default_vars_exported, cd_exports_oldpwd, lua_sees_only_exported, embedder_vars_exported and the_handler_sees_only_exported_variables. cargo fmt --check is clean. cargo clippy --workspace --all-targets exits 0 and none of its warnings are on lines this PR touches.
  • CI: every completed check passes. The macOS jobs were still queued when I looked.
  • Differential: 112 cases against the PR, main and bash 5.2.15. 65 match bash on both, 36 are fixed by the PR, 0 regress, 9 are pre-existing gaps and 2 changed without reaching bash's output. Those 2 are the same local cases as last time.
  • Code that arrived from main:
    • command name runs through run_pipeline, so it is filtered.
    • which (#272) is a builtin and still resolves after export -n PATH.
    • id (#288) reads $USER from its own environment. unset USER; USER=carl; id -un prints lash on the PR and carl on main, which is the intended rule applied to a new command.
    • mktemp ignores an unexported TMPDIR, as bash's does, and honours an exported or prefixed one.
  • Lua: x=1; lua -e 'print(os.getenv("x"))' prints nil on the PR and 1 on main; after export x both print 1.
  • Evidence paths in my first review no longer resolve. The scripts and the non-matching output are inlined below instead.
Appendix: non-blocking (2)
  • ⚪ src/os.rs:901-907: when the shell has at least one unexported variable (a loop counter is enough), keep_exported_only builds a new map and clones every exported value for each command. Main shares the map without copying. Median of 5 runs, 2000 × basename:

    Exported variable PR main
    64 KB 59 ms 52 ms
    1 MB 124 ms 53 ms
    8 MB 606 ms 67 ms
    8 MB, every variable exported 67 ms 67 ms

    Avoiding it means caching the filtered map on the parent, or sharing values instead of copying them. I haven't tried either.

  • The other items from my first review stand as the author answered them. I have nothing new on them.

Reproduction details

Both binaries are release builds from separate target directories: PR fe1fc41 (md5 e5398bc47389…) and main 9851f51 (md5 3bf7e4d9f59e…). Platform is Linux aarch64 with rustc/cargo 1.100.0.

Differential harness (diff.sh), run as PR=… MAIN=… bash diff.sh:

#!/bin/bash
# Differential: PR vs main vs bash for export visibility. Usage: PR=... MAIN=... bash diff.sh
PR=${PR:-/mnt/workspace/pr-shell}; MAIN=${MAIN:-/mnt/workspace/main-shell}
echo "PR=$PR $(md5sum < $PR | cut -c1-12)  MAIN=$MAIN $(md5sum < $MAIN | cut -c1-12)  $(bash --version | head -1)"
run() { local d; d=$(mktemp -d /tmp/diffcase.XXXXXX); (cd "$d" && timeout 20 env -i HOME="$d" PATH=/usr/bin:/bin USER=u "$@" 2>&1 | tr '\n' '|'; echo -n " rc=${PIPESTATUS[0]}"); rm -rf "$d"; }
declare -A tally; n=0
while IFS='|' read -r group title cmd; do
  [ -z "$group" ] && continue; n=$((n+1))
  p=$(run "$PR" -c "$cmd"); m=$(run "$MAIN" -c "$cmd"); b=$(run bash --norc --noprofile -c "$cmd")
  if [ "$p" = "$b" ] && [ "$m" = "$b" ]; then c=MATCH
  elif [ "$p" = "$b" ]; then c=FIXED
  elif [ "$m" = "$b" ]; then c=REGRESSION
  elif [ "$p" = "$m" ]; then c=PRE-EXISTING
  else c=CHANGED; fi
  tally[$c]=$(( ${tally[$c]:-0} + 1 ))
  printf '[%03d] %-12s %s: %s\n' "$n" "$c" "$group" "$title"
  if [ "$c" != MATCH ]; then printf '     $ %s\n     pr  : %s\n     main: %s\n     bash: %s\n' "$cmd" "$p" "$m" "$b"; fi
done < "${CASES:-cases.tsv}"
echo "===== tally ($n cases)"; for k in MATCH FIXED REGRESSION PRE-EXISTING CHANGED; do printf '%-12s %s\n' $k "${tally[$k]:-0}"; done

Cases (cases.tsv, group|title|command):

basic|unexported hidden|x=1; env | grep ^x= || echo hidden
basic|export then assign|export x; x=1; env | grep ^x=
basic|assign then export|x=1; export x; env | grep ^x=
basic|export, unset, reassign|export x=1; unset x; x=2; env | grep ^x= || echo hidden
basic|export -n with value|export -n x=5; env | grep ^x= || echo hidden; echo "[$x]"
basic|export -n after export|export x=1; export -n x; env | grep ^x= || echo hidden; echo "[$x]"
basic|export -n then re-export|export x=1; export -n x; export x; env | grep ^x=
basic|export -n never exported|x=1; export -n x; env | grep ^x= || echo hidden
basic|export -- name|x=1; export -- x; env | grep ^x=
basic|export -n -- name|export x=1; export -n -- x; env | grep ^x= || echo hidden
basic|export multiple|export a=1 b=2 c; c=3; env | grep -E '^[abc]=' | sort
basic|readonly not exported|readonly x=1; env | grep ^x= || echo hidden
basic|readonly then export|readonly x=1; export x; env | grep ^x=
basic|export then readonly|export x=1; readonly x; env | grep ^x=
basic|export -n on readonly|export x=1; readonly x; export -n x; env | grep ^x= || echo hidden
basic|export unset name, later assign|export x; env | grep ^x= || echo hidden; x=1; env | grep ^x=
basic|export unset name, child assigns|export x; sh -c 'x=1; env | grep ^x= || echo hidden'
basic|unset exported, reassign in function|export x=1; f(){ unset x; x=2; env | grep ^x= || echo hidden; }; f
basic|export empty value|export x=; env | grep ^x=
basic|unset -v drops export|export x=1; unset -v x; x=2; env | grep ^x= || echo hidden
basic|= in value|export x=a=b=c; env | grep ^x=
basic|export -n -n|export x=1; export -n -n x; env | grep ^x= || echo hidden
basic|export indirect name|v=x; export $v=1; env | grep ^x=
basic|export in loop|for i in a b; do export v_$i=$i; done; env | grep ^v_ | sort
basic|value with space|export x="a b"; env | grep ^x=
basic|value with newline|export x="$(printf 'a\nb')"; env | grep -c ^x=
basic|set shows unexported|x=1; set | grep ^x=
prefix|prefix visible to env|x=1 env | grep ^x=
prefix|prefix does not persist|x=2; x=1 env | grep ^x=; env | grep ^x= || echo hidden; echo "[$x]"
prefix|prefix only for that command|x=1 env | grep ^x=; env | grep ^x= || echo hidden
prefix|prefix in pipeline stage 2|echo | x=1 env | grep ^x=
prefix|prefix with sh -c|x=1 sh -c 'echo "[$x]"; env | grep ^x='
prefix|prefix with sh -c, inner unset|x=1 sh -c 'unset x; env | grep ^x= || echo hidden'
prefix|prefix overrides exported|export x=0; x=1 env | grep ^x=; env | grep ^x=
prefix|nested sh -c with prefix|x=1 sh -c "sh -c 'env | grep ^x='"
prefix|prefix value from unexported var|y=5; x=$y env | grep ^x=
prefix|prefix plus exported in child|export y=5; x=$y sh -c 'echo "[$x][$y]"'
prefix|two prefixes|x=1 y=2 env | grep -E '^[xy]=' | sort
prefix|prefix on function (single)|f(){ env | grep ^x= || echo hidden; }; x=1 f
prefix|prefix on function (pipeline)|f(){ env | grep ^x= || echo hidden; }; x=1 f | cat
prefix|prefix on eval|x=1 eval 'env | grep ^x= || echo hidden'
prefix|prefix on command|x=1 command env | grep ^x= || echo hidden
prefix|prefix on cd|x=1 cd /tmp; env | grep ^x= || echo hidden
local|local then export|f(){ local x=1; export x; env | grep ^x=; }; f; env | grep ^x= || echo hidden
local|local shadows exported|export x=0; f(){ local x=2; env | grep ^x=; }; f; env | grep ^x=
local|local no value shadows exported|export x=0; f(){ local x; env | grep ^x= || echo hidden; }; f
local|local then export, global existed|x=0; f(){ local x=1; export x; env | grep ^x=; }; f; env | grep ^x= || echo hidden; echo "[$x]"
local|local then export -n, global exported|export x=0; f(){ local x=1; export -n x; env | grep ^x= || echo hidden; }; f; env | grep ^x=
local|callee sees caller's exported local|f(){ local x=1; export x; g; }; g(){ env | grep ^x=; }; f
local|inner exports outer local|f(){ local x=1; g; env | grep ^x=; }; g(){ export x; }; f; env | grep ^x= || echo hidden
local|nested both local|f(){ local x=1; g; }; g(){ local x=2; export x; env | grep ^x=; }; f; env | grep ^x= || echo hidden
local|function export persists|f(){ export x=1; }; f; env | grep ^x=
local|function assignment unexported|f(){ x=1; }; f; env | grep ^x= || echo hidden
local|recursive local export|f(){ local n=$1; export n; [ $n -gt 0 ] && f $((n-1)); env | grep ^n=; }; f 2
local|local + readonly|f(){ local x=1; readonly x; env | grep ^x= || echo hidden; }; f
local|local twice keeps export|f(){ local x=1; export x; local x=2; env | grep ^x=; }; f; env | grep ^x= || echo hidden
local|local reassign keeps export|f(){ local x=1; export x; x=2; env | grep ^x=; }; f
local|unset in function, no local|export x=1; f(){ unset x; }; f; x=2; env | grep ^x= || echo hidden
local|local then unset inside|export x=1; f(){ local x=2; unset x; env | grep ^x= || echo hidden; }; f; env | grep ^x=
local|local export does not leak|f(){ local x=1; export x; }; f; x=3; env | grep ^x= || echo hidden
local|local HOME|f(){ local HOME=/z; env | grep ^HOME=; }; f; env | grep -c ^HOME=
child|sh -c sees exported only|x=1; export y=2; sh -c 'echo "[$x][$y]"'
child|sh -c export does not leak back|sh -c 'export z=1'; env | grep ^z= || echo hidden
child|sh -c inner unexported hidden|sh -c 'z=1; env | grep ^z= || echo hidden'
child|script via sh file|x=1; export y=2; printf 'echo "[$x][$y]"\n' > s.sh; sh s.sh
child|script via shebang|x=1; export y=2; printf '#!/bin/sh\necho "[$x][$y]"\n' > s.sh; chmod +x s.sh; ./s.sh
child|script exports do not leak|printf 'export z=1\n' > s.sh; sh s.sh; env | grep ^z= || echo hidden
child|source sees unexported, exports persist|x=1; printf 'echo "[$x]"; export w=9\n' > s.sh; . ./s.sh; env | grep ^w=
child|eval sees unexported, env hides|x=1; eval 'echo "[$x]"'; eval 'env | grep ^x= || echo hidden'
child|subshell sees var, env hides|x=1; (echo "[$x]"; env | grep ^x= || echo hidden)
child|substitution sees var, env hides|x=1; echo "$(echo "[$x]"; env | grep ^x= || echo hidden)"
child|export -n then subshell|export x=1; export -n x; (env | grep ^x= || echo hidden; echo "[$x]")
child|export inside subshell|(export x=1; env | grep ^x=); env | grep ^x= || echo hidden
child|export inside group|{ export x=1; }; env | grep ^x=
child|export inside substitution|y=$(export x=1; env | grep ^x=); echo "$y"; env | grep ^x= || echo hidden
child|subshell exports parent's var|x=1; (export x; env | grep ^x=); env | grep ^x= || echo hidden
child|nested sh -c exported|export y=1; sh -c 'sh -c "echo [\$y]"'
child|nested sh -c unexported|x=1; sh -c 'sh -c "echo [\$x]"'
child|readonly exported reaches child|readonly r=1; export r; sh -c 'echo "[$r]"'
misc|background unexported|x=1; { env | grep ^x= || echo hidden; } & wait
misc|background exported|export x=1; { env | grep ^x=; } & wait
misc|background prefix|x=1 env | grep ^x= & wait
misc|while-read pipeline export|echo a | while read v; do export v; env | grep ^v=; done
misc|for loop var exported|for i in 1 2; do export i; env | grep ^i=; done
misc|read into exported|export v=old; echo new > f; read v < f; env | grep ^v=
misc|default-assign exported|export v; : ${v:=dflt}; env | grep ^v=
misc|arith assign exported|export n=1; : $((n+=1)); env | grep ^n=
misc|getopts vars not exported|while getopts a o -a; do :; done; env | grep -E '^(OPTIND|o)=' || echo hidden
misc|trap sees unexported hidden|x=1; trap 'env | grep ^x= || echo hidden' EXIT
misc|trap sees exported|export x=1; trap 'env | grep ^x=' EXIT
misc|xargs sh -c unexported|x=1; echo a | xargs -I{} sh -c 'echo "[$x]{}"'
misc|xargs sh -c exported|export x=1; echo a | xargs -I{} sh -c 'echo "[$x]{}"'
misc|find -exec env unexported|x=1; mkdir d; find d -maxdepth 0 -exec env \; | grep ^x= || echo hidden
misc|find -exec env exported|export x=1; mkdir d; find d -maxdepth 0 -exec env \; | grep ^x=
misc|command env unexported|x=1; command env | grep ^x= || echo hidden
misc|command env exported|export x=1; command env | grep ^x=
misc|builtin export -n|export x=1; builtin export -n x; env | grep ^x= || echo hidden
misc|jq env unexported|x=1; jq -n 'env.x'
misc|jq env exported|export x=1; jq -n 'env.x'
misc|mktemp ignores unexported TMPDIR|mkdir zz; TMPDIR=$PWD/zz; mktemp | grep -c zz
misc|mktemp honours exported TMPDIR|mkdir zz; export TMPDIR=$PWD/zz; mktemp | grep -c zz
misc|mktemp honours prefix TMPDIR|mkdir zz; TMPDIR=$PWD/zz mktemp | grep -c zz
start|startup vars exported|env | grep -c -E '^(HOME|PATH|PWD)='
start|cd exports PWD and OLDPWD|mkdir d; cd d; env | grep -c -E '^(PWD|OLDPWD)='
start|child sees PWD after cd|mkdir d; cd d; sh -c 'echo "${PWD##*/}|${OLDPWD:+set}"'
start|HOME reassign stays exported|HOME=/zz; env | grep ^HOME=
start|PATH reassign stays exported|PATH=/usr/bin:/bin:/zz; env | grep ^PATH= | grep -c zz
start|unset HOME, reassign|unset HOME; HOME=/x; env | grep ^HOME= || echo hidden
start|export -n PATH, lookup still works|export -n PATH; ls / >/dev/null && echo ok
start|export -n HOME|export -n HOME; env | grep ^HOME= || echo hidden; echo "${HOME:+set}"
start|unset OLDPWD then cd|mkdir d; unset OLDPWD; cd d; env | grep -c '^OLDPWD='
start|export -n PWD then cd|mkdir d; export -n PWD; cd d; env | grep -c '^PWD='

Every case that doesn't match bash:

[039] PRE-EXISTING prefix: prefix on function (single)
     $ f(){ env | grep ^x= || echo hidden; }; x=1 f
     pr  : hidden| rc=0
     main: hidden| rc=0
     bash: x=1| rc=0
[041] PRE-EXISTING prefix: prefix on eval
     $ x=1 eval 'env | grep ^x= || echo hidden'
     pr  : hidden| rc=0
     main: hidden| rc=0
     bash: x=1| rc=0
[042] PRE-EXISTING prefix: prefix on command
     $ x=1 command env | grep ^x= || echo hidden
     pr  : strands-shell: command: command not found|hidden| rc=0
     main: strands-shell: command: command not found|hidden| rc=0
     bash: x=1| rc=0
[048] CHANGED      local: local then export -n, global exported
     $ export x=0; f(){ local x=1; export -n x; env | grep ^x= || echo hidden; }; f; env | grep ^x=
     pr  : hidden|x=0| rc=0
     main: x=1|x=0| rc=0
     bash: x=0|x=0| rc=0
[055] CHANGED      local: local + readonly
     $ f(){ local x=1; readonly x; env | grep ^x= || echo hidden; }; f
     pr  : hidden|strands-shell: x: readonly variable| rc=0
     main: x=1|strands-shell: x: readonly variable| rc=0
     bash: hidden| rc=0
[059] PRE-EXISTING local: local then unset inside
     $ export x=1; f(){ local x=2; unset x; env | grep ^x= || echo hidden; }; f; env | grep ^x=
     pr  : hidden|x=1| rc=0
     main: hidden|x=1| rc=0
     bash: x=1|x=1| rc=0
[095] PRE-EXISTING misc: command env unexported
     $ x=1; command env | grep ^x= || echo hidden
     pr  : strands-shell: command: command not found|hidden| rc=0
     main: strands-shell: command: command not found|hidden| rc=0
     bash: hidden| rc=0
[096] PRE-EXISTING misc: command env exported
     $ export x=1; command env | grep ^x=
     pr  : strands-shell: command: command not found| rc=1
     main: strands-shell: command: command not found| rc=1
     bash: x=1| rc=0
[099] PRE-EXISTING misc: jq env exported
     $ export x=1; jq -n 'env.x'
     pr  : null| rc=0
     main: null| rc=0
     bash: "1"| rc=0
[111] PRE-EXISTING start: unset OLDPWD then cd
     $ mkdir d; unset OLDPWD; cd d; env | grep -c '^OLDPWD='
     pr  : 1| rc=0
     main: 1| rc=0
     bash: 0| rc=1
[112] PRE-EXISTING start: export -n PWD then cd
     $ mkdir d; export -n PWD; cd d; env | grep -c '^PWD='
     pr  : 1| rc=0
     main: 1| rc=0
     bash: 0| rc=1

Perf (perf2.sh), run as bash perf2.sh <pr-binary> <main-binary>:

#!/bin/bash
# Cost per command of a large EXPORTED variable. doublings: 13=64KB 17=1MB 20=8MB
t() { local s e; s=$(date +%s%N); "$1" -c "$2" >/dev/null 2>&1; e=$(date +%s%N); echo "$(( (e-s)/1000000 ))"; }
loop='i=0; while [ $i -lt 2000 ]; do basename /a/b >/dev/null; i=$((i+1)); done'
mk() { echo "big=aaaaaaaa; k=0; while [ \$k -lt $1 ]; do big=\$big\$big; k=\$((k+1)); done;"; }
for spec in "64KB exported:13:export big;" "1MB exported:17:export big;" "8MB exported:20:export big;" "8MB exported, no unexported vars at all:20:export big i k;" "8MB unexported:20:"; do
  IFS=: read -r label n post <<<"$spec"
  for bin in "$@"; do printf '%-42s %-12s %s ms (median of 5)\n' "$label" "$(basename $bin)" "$(for r in 1 2 3 4 5; do t "$bin" "$(mk $n) $post $loop"; done | sort -n | sed -n 3p)"; done
done

@zastrowm
zastrowm removed the request for review from a team November 19, 2026 01:43
@ferdingler
ferdingler merged commit 2664aa7 into strands-agents:main Nov 20, 2026
33 checks passed
@ferdingler
ferdingler deleted the fix/env-exported-only branch November 20, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants