Free tools Windows power users keep installed

One-click scans. No signup required.

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

In the Linux Foundation Forums example, the inner else is followed directly by fi, with no command in between. That empty branch is the issue identified in the thread. Add the action the branch is meant to perform; however, the posted script contains other malformed syntax, so fixing this one spot alone may not make the full script valid.

Why Bash reports an unexpected fi

Bash uses fi to close an if statement. Its basic structure is a condition, then, commands to run when the condition succeeds, optional elif or else branches, and the closing fi. The GNU Bash Reference Manual documents this syntax.

In the forum post, the nested conditional reaches else and then closes immediately:

else
fi

The parser encounters the closing token where the branch body should be. The thread’s respondent points out that no action was provided after else. The poster reports the resulting message as “syntax error near unexpected token `fi’ on line 7.” See the Linux Foundation Forums thread.

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

How to fix the empty else branch

Put the intended command or commands between else and fi. For example, if the branch should print a message, it could take this form:

else
    echo "No match"
fi

Use the action appropriate to the script; the example message is illustrative, not a reconstruction of the poster’s intended behavior. If the branch is intentionally meant to do nothing, Bash’s no-operation command is ::

else
    :
fi
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the rest of the script

The forum example also contains other malformed syntax. Therefore, adding a command after else addresses the empty branch but does not establish that the complete script is valid. Review each nested if against Bash’s required structure, and then run the script or a syntax check to find any remaining errors.

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.

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