Back to blog

How to Fix Bash Syntax Error Near Unexpected Token Script Errors

When deploying automated shell scripts on Linux servers or running scripts in macOS terminals, you may encounter a syntax crash:

script.sh: line 12: syntax error near unexpected token `in'
script.sh: line 12: `  case $OPTION in'

Alternatively, the console might display: syntax error: unexpected end of file.

While this error suggests a logical syntax typo inside your loop or conditional blocks, the root cause is frequently invisible: carriage return symbols introduced by text editors running on Windows.

In this guide, we will analyze why line endings break Bash parser syntax, and explore three methods to resolve the error.

The Cause: Carriage Returns (CRLF vs. LF)

Different operating systems handle the start of a new line in text files using different hidden characters:

  • Linux and macOS: Use LF (Line Feed, represented as \n).
  • Windows: Uses CRLF (Carriage Return + Line Feed, represented as \r\n).

If you write or edit a shell script on a Windows computer and then clone or upload it to a Linux server:

  1. The Linux Bash interpreter reads the script.
  2. It parses each line looking for instructions. Because the script uses CRLF, every line ends with a hidden carriage return character (\r).
  3. Bash does not ignore \r. It treats the character as part of the command text.
  4. For example, a line containing a simple keyword like done is read by the system as done\r. Since done\r is not a valid shell keyword, the structure of your for loop breaks, triggering the "unexpected token" crash.

Solution 1: Convert Files Using dos2unix

The most reliable way to strip carriage returns from a script is using the standard command-line utility dos2unix.

Step 1: Install dos2unix

If the tool is missing from your system, install it using your system package manager:

  • Ubuntu / Debian:
sudo apt-get install dos2unix
  • macOS (Homebrew):
brew install dos2unix

Step 2: Convert the Script

Run the tool against your target script:

# Convert CRLF line endings to Linux LF format
dos2unix script.sh

This updates the file in place, stripping out all hidden \r characters. The script will now run normally.

Solution 2: Fix Line Endings inside Your IDE (VS Code)

You can configure your text editor to save files using LF line endings natively.

Inside Visual Studio Code:

  1. Open your shell script file.
  2. Look at the status bar in the bottom right corner of the window. You will see either CRLF or LF.
  3. If it displays CRLF, click it and select LF from the dropdown option list.
  4. Save the file.

VS Code will convert all line endings to LF format, allowing the script to execute cleanly on Linux.

Solution 3: Convert File Using sed

If you are running on a server where you cannot install dos2unix, you can use the standard stream editor sed to filter out carriage returns.

  • On Linux:
# Strip carriage returns using sed in-place
sed -i 's/\r$//' script.sh
  • On macOS:
# macOS sed requires an empty string argument for in-place edits
sed -i "" 's/\r$//' script.sh

This regex matches any \r character at the end of a line and replaces it with an empty string, resolving the syntax failure.

Conclusion

The Bash "syntax error near unexpected token" is triggered by hidden carriage return characters (\r) introduced by Windows editors saving files in CRLF format. To fix this, convert your scripts to Unix LF format using dos2unix, toggle the line endings setting inside editors like VS Code, or deploy sed streams to strip out trailing carriage returns.