@@ -19,49 +19,49 @@ public function __construct( $prompt ) {
1919 public function start () {
2020 // @phpstan-ignore while.alwaysTrue
2121 while ( true ) {
22- $ line = $ this ->prompt ();
22+ $ __repl_input_line = $ this ->prompt ();
2323
24- if ( '' === $ line ) {
24+ if ( '' === $ __repl_input_line ) {
2525 continue ;
2626 }
2727
28- $ line = rtrim ( $ line , '; ' ) . '; ' ;
28+ $ __repl_input_line = rtrim ( $ __repl_input_line , '; ' ) . '; ' ;
2929
30- if ( self ::starts_with ( self ::non_expressions (), $ line ) ) {
30+ if ( self ::starts_with ( self ::non_expressions (), $ __repl_input_line ) ) {
3131 ob_start ();
3232 try {
3333 // phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
34- eval ( $ line );
34+ eval ( $ __repl_input_line );
3535 } catch ( \Throwable $ e ) {
3636 // Display the error message but continue the session
3737 fwrite ( STDERR , get_class ( $ e ) . ': ' . $ e ->getMessage () . "\n" );
3838 }
39- $ out = (string ) ob_get_clean ();
40- if ( 0 < strlen ( $ out ) ) {
41- $ out = rtrim ( $ out , "\n" ) . "\n" ;
39+ $ __repl_output = (string ) ob_get_clean ();
40+ if ( 0 < strlen ( $ __repl_output ) ) {
41+ $ __repl_output = rtrim ( $ __repl_output , "\n" ) . "\n" ;
4242 }
43- fwrite ( STDOUT , $ out );
43+ fwrite ( STDOUT , $ __repl_output );
4444 } else {
45- if ( ! self ::starts_with ( 'return ' , $ line ) ) {
46- $ line = 'return ' . $ line ;
45+ if ( ! self ::starts_with ( 'return ' , $ __repl_input_line ) ) {
46+ $ __repl_input_line = 'return ' . $ __repl_input_line ;
4747 }
4848
4949 // Write directly to STDOUT, to sidestep any output buffers created by plugins
5050 ob_start ();
5151 try {
5252 // phpcs:ignore Squiz.PHP.Eval.Discouraged -- This is meant to be a REPL, no way to avoid eval.
53- $ evl = eval ( $ line );
53+ $ __repl_eval_result = eval ( $ __repl_input_line );
5454 } catch ( \Throwable $ e ) {
5555 // Display the error message but continue the session
5656 fwrite ( STDERR , get_class ( $ e ) . ': ' . $ e ->getMessage () . "\n" );
5757 $ evl = null ;
5858 }
59- $ out = (string ) ob_get_clean ();
60- if ( 0 < strlen ( $ out ) ) {
61- echo rtrim ( $ out , "\n" ) . "\n" ;
59+ $ __repl_output = (string ) ob_get_clean ();
60+ if ( 0 < strlen ( $ __repl_output ) ) {
61+ echo rtrim ( $ __repl_output , "\n" ) . "\n" ;
6262 }
6363 echo '=> ' ;
64- var_dump ( $ evl );
64+ var_dump ( $ __repl_eval_result );
6565 fwrite ( STDOUT , (string ) ob_get_clean () );
6666 }
6767 }
@@ -133,8 +133,15 @@ private static function create_prompt_cmd( $prompt, $history_path ) {
133133 $ history_path = escapeshellarg ( $ history_path );
134134 if ( getenv ( 'WP_CLI_CUSTOM_SHELL ' ) ) {
135135 $ shell_binary = (string ) getenv ( 'WP_CLI_CUSTOM_SHELL ' );
136- } else {
136+ } elseif ( is_file ( '/bin/bash ' ) && is_readable ( '/bin/bash ' ) ) {
137+ // Prefer /bin/bash when available since we use bash-specific commands.
137138 $ shell_binary = '/bin/bash ' ;
139+ } elseif ( getenv ( 'SHELL ' ) && self ::is_bash_shell ( (string ) getenv ( 'SHELL ' ) ) ) {
140+ // Only use SHELL as fallback if it's bash (we use bash-specific commands).
141+ $ shell_binary = (string ) getenv ( 'SHELL ' );
142+ } else {
143+ // Final fallback for systems without /bin/bash.
144+ $ shell_binary = 'bash ' ;
138145 }
139146
140147 if ( ! is_file ( $ shell_binary ) || ! is_readable ( $ shell_binary ) ) {
@@ -155,6 +162,21 @@ private static function create_prompt_cmd( $prompt, $history_path ) {
155162 return "{$ shell_binary } -c " . escapeshellarg ( $ cmd );
156163 }
157164
165+ /**
166+ * Check if a shell binary is bash or bash-compatible.
167+ *
168+ * @param string $shell_path Path to the shell binary.
169+ * @return bool True if the shell is bash, false otherwise.
170+ */
171+ private static function is_bash_shell ( $ shell_path ) {
172+ if ( ! is_file ( $ shell_path ) || ! is_readable ( $ shell_path ) ) {
173+ return false ;
174+ }
175+ // Check if the basename is exactly 'bash' or starts with 'bash' followed by a version/variant.
176+ $ basename = basename ( $ shell_path );
177+ return 'bash ' === $ basename || 0 === strpos ( $ basename , 'bash- ' );
178+ }
179+
158180 private function set_history_file () {
159181 $ data = getcwd () . get_current_user ();
160182
0 commit comments