Repository navigation
Description
Checks
- I have updated to the latest minor and patch version of Strands Shell (built from
mainat 2a1495e) - 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 2a1495e)
Language Runtime Version
Rust 1.100.0
Operating System
Debian 12 (aarch64)
Installation Method
built from source
Steps to Reproduce
$ strands-shell -c '{ echo a; echo b; } | while read x; do echo "[$x]"; done'
$ strands-shell -c '( echo a; echo b ) | while read x; do echo "[$x]"; done'
$ strands-shell -c '( echo a ) | { cat; }'
$ strands-shell -c '( echo a ) | ( cat )'
$ strands-shell -c '( echo a ) | if true; then cat; fi'
$ strands-shell -c '( echo a ) | cat | { cat; }'
Expected Behavior
Each runs as it does in bash: [a] [b] for the first two, a for the rest, exit 0.
Actual Behavior
All six are parse errors with exit 1, so nothing in the script runs:
| Script | stderr |
|---|---|
{ echo a; echo b; } | while read x; do …; done |
unexpected 'do' |
( echo a; echo b ) | while read x; do …; done |
unexpected 'do' |
( echo a ) | { cat; } |
expected command after '|' |
( echo a ) | ( cat ) |
expected command after '|' |
( echo a ) | if true; then cat; fi |
unexpected 'then' |
( echo a ) | cat | { cat; } |
expected command |
The same right-hand sides work when the left side is a simple command or a function call:
$ strands-shell -c 'echo a | { cat; }' # a
$ strands-shell -c 'echo a | while read x; do echo "[$x]"; done' # [a]
$ strands-shell -c 'f() { echo a; echo b; }; f | while read x; do echo "[$x]"; done' # [a] [b]
$ strands-shell -c '( echo a ) | cat' # a
(cd dir && ls) | while read f; do …; done and { cmd1; cmd2; } | while read … are common in agent-written scripts.
Additional Context
Found while testing #326. After a compound command, the parser collects what follows | as plain tokens and stops at the first reserved word or parenthesis (src/parser.rs:1329-1345 at 2a1495e):
if *i < tokens.len() && tokens[*i] == Token::Pipe {
// Collect trailing pipeline tokens after the pipe
*i += 1;
let mut tail_tokens: Vec<&Token> = Vec::new();
while *i < tokens.len() {
match &tokens[*i] {
Token::Semi | Token::And | Token::Or | Token::Amp => break,
t if is_reserved(t) => break,
Token::LParen | Token::RParen => break,
...
if tail_tokens.is_empty() {
return Err("expected command after '|'".into());
}
let tail = build_pipeline(&tail_tokens)?;So the tail can only be simple commands: it is empty when the next stage starts with {, (, while or if, and it stops early when a later stage does.
Possible Solution
Parse the stages after a compound command's | the same way as the stages after a simple command's | (parse_pipeline_into_compound, src/parser.rs:1602), which already accepts loops, groups and subshells.
Related Issues
#207 (its "a loop or group can't come after a pipe" item covered a simple command on the left)
Activity
test: reproduce negated compound pipe under set -e and here-doc order…
fix(parser): keep a negated compound pipe visible to set -e and read …
test: pin compound piped into compound inside $( ), redirects and pip…
test: run the redirected compound pipe test in process to fit the Win…