You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[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
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.
Activity