Bash error: "unexpected EOF while looking for matching `)`"
Fix "unexpected EOF while looking for matching )" in Bash command substitution. Usually caused by unbalanced quotes inside $(...). Copy-paste fix.
Error String
bash: unexpected EOF while looking for matching `)'
Tldr
This parse error means Bash hit end-of-input while still inside a $(...) command substitution because a quote or parenthesis inside it was never closed. Count your quotes carefully, or switch the inner string to a different quote style so it does not collide with the outer one.
Cause
Command substitution $(...) is parsed as a nested command, so quotes inside it must balance independently, but a stray unescaped quote character anywhere in the surrounding line can confuse the parser into treating text after it as still being inside the substitution, so it never finds a matching close-paren before EOF. The classic trigger is mixing single quotes inside a $() that is itself inside single quotes, or an unescaped apostrophe inside a single-quoted string.
Repro
#!/usr/bin/env bash
echo "Result: $(echo 'it's broken)"
# bash: -c: line 1: unexpected EOF while looking for matching `)'Fix
#!/usr/bin/env bash
set -euo pipefail
# Use double quotes inside, since the outer text is double-quoted
echo "Result: $(echo "it's fine")"
# Or escape the apostrophe within single quotes: close, escaped quote, reopen
echo "Result: $(echo 'it'\''s fine')"
# Or use #39;' ANSI-C quoting to avoid the collision entirely
echo "Result: $(echo #39;it\'s fine')"Explanation
Bash parses quotes left to right without understanding intent — an apostrophe used as punctuation inside single quotes closes the quoted string early. Prefer double quotes for strings containing apostrophes, or run the substitution through `bash -n` to catch the imbalance before running the full script.
Faq
Q
Why does the error mention EOF instead of pointing at the bad line?
A
Because the parser is still waiting for a closing paren/quote when it runs out of input entirely — from its perspective, everything after the unbalanced quote is still part of the unfinished command substitution, so it can only report the failure at end of file.
Q
How do I safely embed an apostrophe inside single quotes?
A
Close the quote, add an escaped literal quote, then reopen: 'it'\''s' produces "it's". It looks awkward but is the standard POSIX-safe pattern.
Q
Can ShellCheck catch this before I run the script?
A
Yes — ShellCheck parses quoting structure statically and will flag unbalanced quotes with SC1078/SC1079-style warnings, catching the mistake without executing anything.