Bash error: unexpected EOF while looking for matching quote

Bash reports unmatched quotes when a string opens with " or ' but never closes. Find the unbalanced quote with `bash -n`.

Error String

bash: unexpected EOF while looking for matching `"'

Tldr

A double or single quote was opened somewhere in the script and never closed. Bash reads to EOF still in "inside a string" mode and gives up. `bash -n script.sh` tells you exactly where to look.

Cause

Common patterns: an embedded apostrophe inside a single-quoted string ("don't"), a multi-line string missing the closing quote, or a heredoc that mistakenly uses quotes instead of `<<EOF`.

Repro

echo 'don't do this'         # unmatched single quote

Fix

# Escape with double quotes around the apostrophe:
echo "don't do this"

# Or use the 'concat' trick for a single-quoted literal:
echo 'don'"'"'t do this'

Explanation

For text with mixed quotes, the safest pattern is to switch to double quotes and escape what needs escaping, or use a heredoc with `<<'EOF'` to disable all expansion.

Faq

Q

Can I escape a single quote inside single quotes?

A

No. End the string, add `\'`, and reopen it — or use double quotes or `

#39;...'`.

Q

Why does <<- not strip my indentation?

A

`<<-` removes leading tabs only. Spaces are kept, so the terminator must still match exactly.

Deep Dive

Heading

Unbalanced quotes and brackets

Body

The message names the character Bash was waiting for — usually a quote, backtick, brace or parenthesis. A single unclosed double quote swallows the rest of the file, which is why the reported line is the last one. Apostrophes inside single-quoted strings are the classic trigger: `echo 'don't'` closes the string early. Use double quotes with an escaped apostrophe, or `

#39;don\'t'`, or simply switch quote styles.

Heading

Heredocs need an exact terminator

Body

A heredoc terminator must be at the start of its line with no trailing whitespace, unless you opened it with `<<-`, which strips leading tabs only — not spaces. Quoting the delimiter (`<<'EOF'`) prevents expansion inside the body, which also removes a whole class of parse surprises when the body contains backticks or `$(`. Indented heredocs pasted from a formatter are a frequent cause of this error in otherwise valid scripts.

Checklist

Check the newest string literal for an unbalanced quote.

Verify heredoc terminators sit in column one with no trailing spaces.

Use `<<'EOF'` when the body should not be expanded.

Run `bash -n` and let syntax highlighting show where the string starts.