Skip to content

Commit 90d42bc

Browse files
committed
fix(interpreter): set BASH_SOURCE[0] when running bash /path/script.sh
Closes #942
1 parent 183077a commit 90d42bc

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
@@ -2686,8 +2686,20 @@ impl Interpreter {
26862686
self.pipeline_stdin = stdin.clone();
26872687
}
26882688

2689+
// Set BASH_SOURCE for script file execution
2690+
if let Some(ref file) = script_file {
2691+
self.bash_source_stack.push(file.clone());
2692+
self.update_bash_source();
2693+
}
2694+
26892695
let result = self.execute(&script).await;
26902696

2697+
// Restore BASH_SOURCE
2698+
if script_file.is_some() {
2699+
self.bash_source_stack.pop();
2700+
self.update_bash_source();
2701+
}
2702+
26912703
// Restore stdin
26922704
self.pipeline_stdin = saved_stdin;
26932705

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)