Skip to content

Commit 2924c05

Browse files
committed
fix(interpreter): set BASH_SOURCE[0] when running bash /path/script.sh
Closes #942
1 parent 8c03e30 commit 2924c05

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
@@ -2680,8 +2680,20 @@ impl Interpreter {
26802680
self.insert_variable_checked("OPTIND".to_string(), "1".to_string());
26812681
self.variables.remove("_OPTCHAR_IDX");
26822682

2683+
// Set BASH_SOURCE for script file execution
2684+
if let Some(ref file) = script_file {
2685+
self.bash_source_stack.push(file.clone());
2686+
self.update_bash_source();
2687+
}
2688+
26832689
let result = self.execute(&script).await;
26842690

2691+
// Restore BASH_SOURCE
2692+
if script_file.is_some() {
2693+
self.bash_source_stack.pop();
2694+
self.update_bash_source();
2695+
}
2696+
26852697
// Restore state
26862698
if let Some(val) = saved_optind {
26872699
self.insert_variable_checked("OPTIND".to_string(), val);
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)