diff --git a/crates/bashkit/src/interpreter/mod.rs b/crates/bashkit/src/interpreter/mod.rs index 346fd3a8..5b77bb7f 100644 --- a/crates/bashkit/src/interpreter/mod.rs +++ b/crates/bashkit/src/interpreter/mod.rs @@ -2680,8 +2680,17 @@ impl Interpreter { self.insert_variable_checked("OPTIND".to_string(), "1".to_string()); self.variables.remove("_OPTCHAR_IDX"); + // Forward piped stdin to child when executing a script file or -c command + let saved_stdin = self.pipeline_stdin.take(); + if script_file.is_some() || is_command_mode { + self.pipeline_stdin = stdin.clone(); + } + let result = self.execute(&script).await; + // Restore stdin + self.pipeline_stdin = saved_stdin; + // Restore state if let Some(val) = saved_optind { self.insert_variable_checked("OPTIND".to_string(), val); diff --git a/crates/bashkit/tests/spec_cases/bash/bash-stdin-pipe.test.sh b/crates/bashkit/tests/spec_cases/bash/bash-stdin-pipe.test.sh new file mode 100644 index 00000000..af6502e1 --- /dev/null +++ b/crates/bashkit/tests/spec_cases/bash/bash-stdin-pipe.test.sh @@ -0,0 +1,22 @@ +### bash_builtin_stdin_pipe_to_script +# echo | bash script.sh should forward stdin +echo 'echo "got: $(cat)"' > /tmp/stdin_test.sh +echo "hello" | bash /tmp/stdin_test.sh +### expect +got: hello +### end + +### bash_builtin_stdin_pipe_to_c +# echo | bash -c 'cat' should forward stdin +echo "piped" | bash -c 'cat' +### expect +piped +### end + +### bash_builtin_stdin_read +# echo | bash script.sh with read builtin +echo 'read -r line; echo "line: $line"' > /tmp/read_test.sh +echo "test input" | bash /tmp/read_test.sh +### expect +line: test input +### end