Skip to content

[BUG] command NAME ARGS expands its arguments twice, so substitutions run twice #364

Open

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell
  • 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

main @ dbb6189 (crate version 0.0.0); also present on #352 head b92a56e

Language Runtime Version

Rust 1.100.0

Operating System

Debian 12 (aarch64)

Installation Method

built from source

Steps to Reproduce

Run each in an empty directory with the CLI (cargo build --features cli --bin strands-shell):

strands-shell -c 'command echo $(echo once >> count; echo x); cat count'
strands-shell -c 'n=0; command echo $((n += 1)) "$n"'
strands-shell -c 'printf "l1\nl2\n" | { command echo "got: $(cat)"; }'

Expected Behavior

Same as bash 5.2.15, where each argument is expanded once:

x
once
1 1
got: l1
l2

Actual Behavior

x
once
once
2 2
got: 

The substitution runs twice. In the third case the first run consumes stdin and its result is thrown away, so the command gets an empty string, with exit 0 and nothing on stderr.

Additional Context

This is the command half of #261, which noted that command "has the same expand-then-dispatch shape and is worth checking". #352 fixes the exec arm only and will close #261.

src/exec.rs:3516 (main @ dbb6189) expands the arguments to look for -v/-V, then src/exec.rs:3550-3554 passes the unexpanded words to execute_pipeline, which expands them again:

let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await;
...
let fake_cmd = parser::Command {
    env: pipeline[0].env.clone(),
    args: pipeline[0].args[1..].to_vec(),
    redirects: pipeline[0].redirects.clone(),
};

builtin command echo $(...) is affected the same way. command -v / -V return before the second expansion and are not affected.

Possible Solution

The same change #352 makes for exec: build fake_cmd.args from the already expanded args, each as a single WordPart::SingleQuoted word. A differential case next to exec_expands_args_once.sh would cover it.

Related Issues

#261, #352

Activity

  1. added
    bug
    on Nov 21, 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