File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff 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) ;
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments