The Linux Foundation Forums example reports syntax error near unexpected token `fi' because an inner else is followed immediately by fi. Bash requires at least one command between those keywords. Add the intended action, or use the no-op command : if that branch should deliberately do nothing. The post also contains other malformed syntax, so correcting this empty branch alone may not make the entire script valid.
Why Bash points at fi
In Bash, an if construct has this order: a condition, then, one or more commands, an optional elif or else, more commands, and the closing fi. The GNU Bash Reference Manual documents that grammar at GNU Bash Reference Manual.
In the forum post, the nested block reaches else and then immediately reaches fi. At that point the parser is still waiting for a command to execute in the else branch. Since fi is a block terminator rather than a command, Bash reports it as the unexpected token. The line number identifies where the parser finally becomes unable to continue; the missing command is just before it.
The required shape of an if block
if condition; then
command_for_true_case
else
command_for_false_case
fi
Both branches in this example contain commands. A branch can contain several commands, and an else clause is optional, but an else that is present cannot be empty.
#1 Best Overall
If the branch should do nothing
if condition; then
command_for_true_case
else
:
fi
The standalone : is Bash’s no-op command. It gives the branch a valid command without changing state. Use it only when doing nothing is genuinely the intended behavior; otherwise replace it with the real action, such as an assignment, logging statement, or command.
What is wrong in the forum snippet
The reported code has an outer if and a nested if. In the nested block, the sequence is effectively:
Rank #2
then
$1 -n $3
else
fi
The empty else explains the reported fi error. However, $1 -n $3 is also not visibly a complete test expression in standard Bash syntax. Bash treats the first expanded word as the command to run; it does not automatically interpret that line as a bracketed string test. The intended test or command must be reconstructed from what the script is meant to do.
For example, if the intention was to test whether the third positional parameter is a non-empty string, the test would normally be written with Bash’s conditional syntax:
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsif [ -n "$3" ]; then
command_for_nonempty_value
else
command_for_empty_value
fi
This is only an example of valid structure, not a guaranteed replacement for the forum author’s intended logic. The outer condition, quoting, and commands must match the script’s purpose.
How to repair the script safely
- Fill the inner
else. Add the command that should run when the nested condition is false. If no action is required, add:. - Make the preceding condition a valid command. If it is a test, use a valid form such as
[ ... ]or Bash’s[[ ... ]], with the required spacing. - Check each
then. Athenmust follow its condition, either on the next line or after a semicolon. - Match every block terminator. Each nested
ifneeds its ownfi, and the closing statements must appear in the reverse order in which the blocks opened. - Run a syntax-only check before executing the script. Use
bash -n path/to/script.sh. This parses the file without running its commands, allowing additional syntax errors to be found safely.
Common mistakes that produce a similar message
| Pattern | What Bash needs |
|---|---|
else followed directly by fi |
At least one command in the else branch, or : for an intentional no-op |
if condition with no then |
then on the next line or separated by a semicolon |
| Test operators written as bare words | A complete test command, with correct brackets and spacing |
| Nested blocks closed in the wrong order | One matching fi for each if, inner block first |
Bottom line for this Linux Foundation Forums error
The immediate cause is the empty inner else: Bash encounters fi where a command is required. Add the missing action or a deliberate no-op, then run bash -n and fix the remaining malformed condition and any other parser errors. The original thread is available at Linux Foundation Forums.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




