Skip to content

awk: else on the line after } or ; is rejected #170

Open

Description

@sylvestre

POSIX allows an optional ; and newlines between the if body and else.
uutils awk accepts if (c) stmt NEWLINE else, but not when the body ends
with } or ;, which is the usual way to write it:

if (x) {
    ...
}
else {
    ...
}

Reproduction

Rust (incorrect)

$ ./target/release/awk 'BEGIN { if (0) { print "yes" }
else print "no" }'
Error: Syntax error
   ╭─[ CLI:2:1 ]
 2 │ else print "no" }
   │   ╰──── Unexpected token: invalid statement start.

GNU (correct)

$ gawk 'BEGIN { if (0) { print "yes" }
else print "no" }'
no

if (0) print 1; NEWLINE else print 2 fails the same way.

Test

#[test]
fn else_on_next_line_after_brace() {
    ucmd()
        .arg("BEGIN { if (0) {\n  print \"a\"\n}\nelse {\n  print \"b\"\n} }")
        .succeeds()
        .stdout_only("b\n");
}

#[test]
fn else_on_next_line_after_semicolon() {
    ucmd()
        .arg("BEGIN { if (0) print \"a\";\n\nelse print \"b\" }")
        .succeeds()
        .stdout_only("b\n");
}

Exposed by the GNU awk test suite: compare2, funstack, intformat, numindex,
sortu, timeout.

Activity

  1. added
    bug
    good first issue
    gnu-compat
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions