Repository navigation
fix: report the first syntax error in multiline input - #197
Merged
ferdingler merged 2 commits intoNov 4, 2026
Merged
Conversation
Multiline input, and every sourced or sh script, is parsed one statement at a time. When a statement did not parse, the executor read the next line and tried again, as if the statement was incomplete. A real syntax error was then hidden: for `echo one; )` followed by a line with an open quote, the shell reported the unterminated quote instead of the `)`, with no line. The executor now tells a definite syntax error from input that is only incomplete. It stops at the first definite error, reports the line where it found it, and returns status 1 as before. A line that ends in a backslash still joins the next line. Incomplete input at the end of the text keeps its old message.
kevin-orellana
approved these changes
Nov 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Multiline input (anything passed to
runwith a newline in it, plus every sourced orshscript) is parsed a statement at a time, and when a statement didn't parse the shell assumed it was unfinished and kept reading lines. So a real syntax error got buried under whatever came later: withecho one, then), thenecho "two, you got told about the open quote with no line number instead of the)on line 2.Now the shell tells a real syntax error apart from input that just isn't finished yet, stops at the first real error and reports the line it was on, like bash does, and the exit status stays 1. Unfinished input at the very end still gets the same message as before, a line ending in a backslash still continues, and single-line input is unchanged, so the visible change is that multiline syntax errors now start with the line number.
Evidence of how this is fixed in Box
Strands Box carries a vendored copy of Shell and partly fixed this: it also stops at the first real error and names its line (UPSTREAM.md note). But a line ending in a backslash before a keyword (
for i in a; \thendo …) fails to parse there, and 3 of this PR's 6 tests pass on it.Related Issues
None.
Documentation PR
None.
Type of Change
Bug fix
Testing
I ran about thirty valid multiline shapes on main and with the change, and the only differences were scripts that already failed, which now fail earlier and name a line. A review caught that a backslash before a keyword like
dobroke, so I fixed that and added a test, and the new tests fail without the fix while the Rust, Python and Node suites pass. Separate bugs I found and left alone: a here-doc inside a multilineifor loop is also parsed as code,f()with{on the next line doesn't parse, andfalse &&at the end of a line still runs the next line.cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.