Skip to content

[BUG] Inside $( ), a redirected group or a piped group, { …; } | cmd keeps the group's variables and cd ({ { cd d; ls; } | cat; } > out leaves the shell in d) #399

Backlog

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (built from main at 4069bf9)
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Binding

Rust

Strands Shell Version

0.0.0 (built from source, main 4069bf9)

Language Runtime Version

Rust 1.100.0

Operating System

Debian 12 (aarch64)

Installation Method

built from source

Steps to Reproduce

$ strands-shell -c 'x=0; r=$( { x=1; echo a; } | cat; echo "x=$x" ); echo "$r"'
$ strands-shell -c 'x=0; { { x=1; echo a; } | cat; echo "x=$x"; } | cat'
$ strands-shell -c 'mkdir d; { { cd d; echo a; } | cat; } > out; cat out'

Expected Behavior

As in bash 5.2.15, every stage of a pipeline runs in its own subshell, so the group's x=1 and cd d end with it: a x=0, then a x=0, then a.

Actual Behavior

Script stdout status
x=0; r=$( { x=1; echo a; } | cat; echo "x=$x" ); echo "$r" a x=1 0
x=0; { { x=1; echo a; } | cat; echo "x=$x"; } | cat a x=1 0
mkdir d; { { cd d; echo a; } | cat; } > out; cat out nothing; stderr cat: no such file or directory: <cwd>/d/out 1

In the third script the cd d outlives the pipeline and the redirected group, so the rest of the script runs in d. The first two scripts give the same result through Shell::run.

These are correct:

$ strands-shell -c 'x=0; { x=1; echo a; } | cat; echo "x=$x"'                    # a, x=0  (top level)
$ strands-shell -c 'x=0; r=$( ( x=1; echo a ) | cat; echo "x=$x" ); echo "$r"'   # a, x=0  (subshell on the left)

So it needs a { …; } group (or a loop, if or case) on the left of the pipe, in a place where output is captured.

Additional Context

Found while reviewing #359. With that PR, x=0; echo "$( { x=1; echo a; } | { cat; echo "x=$x"; } )" prints x=1 for the same reason.

In src/exec.rs at 4069bf9, the capturing executor runs the compound of a CompoundPipeline on the caller's process (lines 2178-2180):

let compound_cl = vec![(*compound.clone(), None)];
let (captured, result) =
    Box::pin(run_capturing_status(os.clone(), proc, &compound_cl)).await;

The direct path forks first (lines 2474-2483: let mut sub = proc.fork(); then run_capturing_status(os2, &mut sub, &compound_cl)), which is why the top-level form is correct.

I did not test the Python, Node.js or WASM bindings.

Possible Solution

Run the compound on a fork of proc in the capturing arm too, and carry back what the direct path carries back (status, captured stderr, unread stdin). Not tried.

Related Issues

#359, #383

Activity

  1. added
    bug
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions