Bash Error Guide: 'unexpected EOF while looking for matching quote' — Fix It
Fix Bash 'unexpected EOF while looking for matching quote': close every quote, escape apostrophes correctly, and use printf %q to spot the unbalanced quote.
- #bash
- #automation
- #troubleshooting
- #errors
Stuck on this Bash & Python Automation error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Overview
Bash reports unexpected EOF while looking for matching quote when it opens a " or ' and reaches the end of the input still inside the string:
./run.sh: line 60: unexpected EOF while looking for matching `"'
The backtick-and-quote in the message names the quote character Bash is still waiting to close. Because everything after the stray opening quote is swallowed as string content, the error is reported at end-of-file, not where the unbalanced quote actually is. The real bug is an opening quote with no partner — very often an apostrophe inside a single-quoted string.
Symptoms
- The error points at the last line of the script or a here-document, far from the mistake.
- Interactively, your prompt changes to the
>continuation prompt and waits for more input — Bash still thinks the string is open. - It appears after adding a message containing an apostrophe (
don't,it's) inside'...'. bash -n script.shreports the same error without executing anything.
Common Root Causes
- Apostrophe inside single quotes —
echo 'don't do that'closes the string atdon, leavingt do that'unbalanced. - A missing closing
"or'somewhere in a long command or string. - Quotes inside command substitution — an unbalanced quote within
$( ... ). - A quote split across a bad line continuation — a
\that broke a quoted string incorrectly. - Pasting text with a fancy/smart quote (
’“) that isn’t an ASCII quote and never matches. - Mismatched quote types — opening with
"and trying to close with'.
Diagnostic Workflow
Syntax-check without running:
bash -n ./run.sh
Count quotes per line to find the imbalance (odd count = unbalanced):
awk '{ n=gsub(/'\''/,"&"); if (n%2) print NR": odd single-quotes -> "$0 }' run.sh
awk '{ n=gsub(/"/,"&"); if (n%2) print NR": odd double-quotes -> "$0 }' run.sh
Reveal smart quotes or hidden characters:
grep -nP "[\x{2018}\x{2019}\x{201C}\x{201D}]" run.sh # curly quotes
Interactively, if you’re stuck at the > prompt, press Ctrl-C to abort rather than adding more quotes.
Example Root Cause Analysis
A notification helper broke after a copy edit:
notify 'Deploy finished — don't forget to smoke-test'
Running it hung, then on Ctrl-D reported:
notify.sh: line 41: unexpected EOF while looking for matching `''
The apostrophe in don't closed the single-quoted string early: Bash saw 'Deploy finished — don', then t forget to smoke-test' as more tokens, leaving a dangling '. Inside single quotes you cannot escape an apostrophe — you must close, insert an escaped quote, and reopen, or switch to double quotes:
# Option A: close-escape-reopen the apostrophe
notify 'Deploy finished — don'\''t forget to smoke-test'
# Option B: use double quotes (apostrophe is literal there)
notify "Deploy finished — don't forget to smoke-test"
Option B is the readable fix here since the message has no $ or backticks that double quotes would expand.
Prevention Best Practices
- Use double quotes for text containing apostrophes — inside
"..."an apostrophe is just a character. - For a literal apostrophe inside single quotes, use the
'\''idiom (close, escaped quote, reopen). - Syntax-check in CI —
bash -nand ShellCheck catch unbalanced quotes before deploy. - Watch for smart quotes when pasting from docs or chat; normalize to ASCII
'and". - Keep long strings in here-documents (
<<'EOF' ... EOF) where quoting rules are simpler. - Let your editor’s syntax highlighting flag the run-on string visually.
Quick Command Reference
bash -n ./script.sh # syntax check, no execution
shellcheck ./script.sh # pinpoints the unbalanced quote
printf '%q\n' "$var" # reveal quote characters in a value
awk '{n=gsub(/"/,"&"); if(n%2)print NR}' script.sh # lines with odd quotes
grep -nP "[\x{2018}\x{2019}]" script.sh # find smart quotes
# literal apostrophe inside single quotes:
echo 'it'\''s fine'
Related Guides
- Bash Error Guide: ‘syntax error: unexpected end of file’ — the sibling error for unclosed blocks and here-documents.
- Bash Error Guide: ‘syntax error near unexpected token’ — other quoting and parse failures reported mid-file.
- Bash Error Guide: ‘bad substitution’ — related parse-time failures inside expansions.
Conclusion
unexpected EOF while looking for matching quote means a quote opened and never closed, so Bash consumed the rest of the file as a string. The message tells you which quote character is dangling; the usual culprit is an apostrophe inside single quotes. Use double quotes for apostrophe-bearing text, the '\'' idiom when you must stay single-quoted, and run bash -n plus ShellCheck in CI so unbalanced quotes never reach production.
Fixed it? Get 500 Bash & Python Automation & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.