Skip to content

parser: trailing comment (or body) on the f() { line -> "syntax error near unexpected token (" #443

Closed

Description

@leeefun

Summary

A function definition whose opening { line carries any trailing token after the brace — a # comment, or the start of the body — is rejected by the parser:

syntax error near unexpected token `('
f ( ) { # usage

GNU Bash accepts all of these. Note the error is reported against the mangled rendering f ( ) {, and it fires before any execution.

Minimal reproduction

a.sh:

f() { # usage
  echo hi
}
f
$ niu a.sh
a.sh: line 1: syntax error near unexpected token `('
a.sh: line 1: `f ( ) { # usage'
$ echo $?
2

# GNU Bash 5.2.21 / 5.3.0:
$ bash a.sh
hi
$ echo $?
0

Scope (measured)

Same file, niu vs GNU:

body form (line 1) niu GNU
f() { # c (comment after brace) syntax error ok
f() { # (empty comment) syntax error ok
f() { echo hi (body starts on the brace line, closes later) syntax error ok
f() { echo hi # c (body + trailing comment) syntax error ok
f() { x=1 # c (assignment + comment on brace line) syntax error ok
f() { echo hi; } (complete one-liner) ok ok
f() { (nothing after brace, body on following lines) ok ok
f() then { on its own line, body next line ok ok
f() then { echo hi on the next line ok ok

Control: trailing comments are fine on other compound openers — if true; then # c and for i in 1; do # c both parse.

So the trigger is specific to the function-definition brace f() { <anything other than a sole newline>. A trailing comment is the common real-world case (e.g. usage() { # <description>), which is why this surfaced: any script using per-function doc comments fails to run at all.

Environment

Niubash 1.1.2 — bash-compatible shell for Windows
  rubash   git b4cbcadc7c1a
  winuxcmd WinuxCmd 1.0.7

(Engine-level parse issue; reporting here as the owner of the lexer/parser. Happy to have it moved to niubash if you prefer.)

Expected

All rows above parse and run identically to GNU Bash — comments after { are lexed as comments, and a non-empty body that opens on the f() { line continues to the matching }.

Impact

Blocks any Bash script that writes function doc-comments on the { line — a very common style. Was discovered while running a repo's tools/ci/*.sh under niu (fell back to WSL GNU Bash to confirm the scripts themselves were valid).

Activity

  1. reopened this on Oct 6, 2026
  2. leeefun commented on Oct 6, 2026

    @leeefun
    Author

    补充:本 issue 此前被误关闭为 completed,但截至当前 master 并无对应修复提交(git log 中没有引用 #443 的 fix,lexer/parser 最近的改动都与函数花括号无关),且在 Niubash 1.1.2(rubash b4cbcad)上仍 100% 复现。故重新打开。

    复现:

    printf 'f() { # usage\n  echo hi\n}\nf\n' > a.sh
    bash a.sh   # GNU Bash:输出 hi,rc=0
    niu  a.sh   # rubash:syntax error near unexpected token `(',rc=2

    触发面(实测):f() { 之后只要不是纯换行就解析失败 —— # 注释、正文本体、赋值+注释都中招;f() { 单独一行、或 f() { ...; } 整行闭合则正常。其它复合入口(if ...; then # c、for ...; do # c)不受影响。

    (面向用户的同一条见 unixwin/niubash 的对应 issue。)

  3. added
    P1-high
    on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P1-high

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions