DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

Unexpected token `fi` in Bash: Fixing the Linux Foundation Forums Error

Bash reports an unexpected `fi` when an `else` branch has no command. Here is the exact fix, a valid `if` structure, and the other syntax issue visible in the forum example.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
if [ -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

  1. Fill the inner else. Add the command that should run when the nested condition is false. If no action is required, add :.
  2. 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.
  3. Check each then. A then must follow its condition, either on the next line or after a semicolon.
  4. Match every block terminator. Each nested if needs its own fi, and the closing statements must appear in the reverse order in which the blocks opened.
  5. 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
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.