Skip to content

Commit 7259df4

Browse files
authored
fix(interpreter): set BASH_SOURCE[0] when running bash /path/script.sh (#1037)
## Summary - Push script file path onto `bash_source_stack` before executing via `bash /path/script.sh` - Pop after execution completes ## Test plan - [x] Spec tests: BASH_SOURCE[0] value, dirname pattern - [x] Full test suite passes Closes #942
1 parent aeb21c1 commit 7259df4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

crates/bashkit/src/interpreter/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,8 +2759,20 @@ impl Interpreter {
27592759
self.pipeline_stdin = stdin.clone();
27602760
}
27612761

2762+
// Set BASH_SOURCE for script file execution
2763+
if let Some(ref file) = script_file {
2764+
self.bash_source_stack.push(file.clone());
2765+
self.update_bash_source();
2766+
}
2767+
27622768
let result = self.execute(&script).await;
27632769

2770+
// Restore BASH_SOURCE
2771+
if script_file.is_some() {
2772+
self.bash_source_stack.pop();
2773+
self.update_bash_source();
2774+
}
2775+
27642776
// Restore stdin
27652777
self.pipeline_stdin = saved_stdin;
27662778

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### bash_source_in_executed_script
2+
# BASH_SOURCE[0] should equal script path when run via bash
3+
echo 'echo "${BASH_SOURCE[0]}"' > /tmp/bsrc_test.sh
4+
bash /tmp/bsrc_test.sh
5+
### expect
6+
/tmp/bsrc_test.sh
7+
### end
8+
9+
### bash_source_dirname_pattern
10+
# Common pattern: find script's own directory
11+
echo 'echo "$(dirname "${BASH_SOURCE[0]}")"' > /tmp/bsrc_dir.sh
12+
bash /tmp/bsrc_dir.sh
13+
### expect
14+
/tmp
15+
### end

0 commit comments

Comments
 (0)