bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] add semicolon if last word in command group ends in &


From: Grisha Levit
Subject: [PATCH] add semicolon if last word in command group ends in &
Date: Sat, 10 Jun 2023 19:31:18 -0400

The command printing code can fail to add a required semicolon when the
last word in the command ends with `&'

$ bash --pretty-print <<<$'{ \&;}'
{ \& }

$ f() { if echo \&; then :; fi; }
$ declare -f f
f ()
{
    if echo \& then
        :;
    fi
}
$ eval "$(declare -f f)"
bash: syntax error near unexpected token `fi'
---
diff --git a/print_cmd.c b/print_cmd.c
index 29870837..961c8dae 100644
--- a/print_cmd.c
+++ b/print_cmd.c
@@ -1446,8 +1446,10 @@ indent (int amount)
 static void
 semicolon (void)
 {
-  if (command_string_index > 0 &&
-       (the_printed_command[command_string_index - 1] == '&' ||
+  if ((command_string_index > 1 &&
+       the_printed_command[command_string_index - 2] == ' ' &&
+       the_printed_command[command_string_index - 1] == '&') ||
+      (command_string_index > 0 &&
        the_printed_command[command_string_index - 1] == '\n'))
     return;
   cprintf (";")


reply via email to

[Prev in Thread] Current Thread [Next in Thread]