Skip to content

[parse] terminator-less 'then' after ]] inside a function body rejected — GNU accepts (breaks shell_function_n_shortcuts/.shortcut) #460

Closed

Description

@caomengxuan666

Construct (minimal repro)

f() {
  if [[ $x == y ]] then
    echo a
  fi
}

rubash -n (fresh master a157656, release build):

tc.sh: line 2: syntax error near unexpected token `then'

GNU bash 5.3.0 accepts this file. bash -n at top level WITHOUT the function wrapper (if [[ $x == y ]] then ... fi) is accepted by rubash, and the ;-terminated form (if true; then) inside a function is accepted — only the terminator-less then directly after ]] inside a function body is rejected. The same construct in a while/until condition or at top level parses; a plain if cmd then (non-conditional) is rejected the same way as GNU.

Real-world evidence (ecosystem harvest)

GaelGosse/shell_function_n_shortcuts/.shortcut (battery-status prompt function), harvest 2026-10-04:

	if [[ $charge == "Discharging" ]] then
		battery+="🌙 "
	elif [[ $charge == "Full" ]] then
  • WSL GNU Bash 5.3.0 /usr/local/bin/bash -n: rc=0 (recorded oracle, harvest run 2026-10-04/05)
  • Git Bash bash-5.2 grammar: rc=0 (non-authoritative corroboration)
  • rubash master a157656 rubash -n: rc=2, syntax error near unexpected token 'then'
  • Asset is an interactive prompt function; parsed clean by GNU.

GNU C source anchor (vendored bash.git @b4608166, Bash-5.3 patch 15)

  • parse.y:1178 if_command: IF compound_list THEN compound_list FI — the condition is a compound_list.
  • parse.y:1262 compound_list: newline_list list0 | newline_list list1 — the list1 alternative requires no list terminator.
  • parse.y:1323-1324 list1: ... | pipeline_command — a single pipeline_command is a complete list1, so [[ ]] followed directly by THEN is grammatical.
  • parse.y:5899 reserved_word_acceptable() — COND_END is in the acceptable-predecessor set, so then IS recognized as the reserved word THEN when it follows ]].

Shrunken 5-line repro (from the 2876-line asset, dual-condition rubash-fails/GNU-grammar-accepts):

PRPT() {
	if [[ $charge == "Discharging" ]] then
		echo "$e_line1"
	fi
}

Classification

Parser divergence (conditional command inside function body, terminator-less THEN). Not covered by rubash#435 (multi-line (( )), fixed in fa87622) — that fix restored the top-level/multi-line arithmetic form; this is the if/elif + [[ ]] + then list-terminator form inside function bodies.

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

    P2-mediumbuggnu-baseline

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions