Linux Bash Syntax Error Near Unexpected Token
About Linux Bash Syntax Error Near Unexpected Token
Bash 'syntax error near unexpected token' occurs due to scripting mistakes like mismatched quotes, missing semicolons, or Windows line endings in scripts. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: One of the most common bash scripting errors. The error message points to the token where bash got confused. Common tokens: '(', ')', 'do', 'done', 'fi', 'then', newline. Windows carriage returns (\r) are invisible but cause this error. Missing or extra quotes, brackets, or keywords cause parsing failures. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Script created/edited on Windows containing \r\n line endings. Missing 'then' after 'if' condition or 'do' after 'for/while'. Mismatched or unclosed quotes (single or double). Missing semicolon before 'then', 'do', or closing keywords. Copy-pasted code containing smart quotes or invisible Unicode characters. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Check for Windows line endings: cat -A script.sh (look for ^M at line ends). Convert line endings: dos2unix script.sh or sed -i 's/\r$//' script.sh. Run bash syntax check without executing: bash -n script.sh. Check for unmatched quotes: grep -n '"' script.sh | wc -l (should be even). Use shellcheck: shellcheck script.sh for comprehensive linting. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
Why do Windows line endings cause bash errors?
Windows uses \r\n (CRLF) while Linux uses \n (LF). The \r character is invisible but bash interprets it as part of the command, causing syntax errors.
Overview
Bash 'syntax error near unexpected token' occurs due to scripting mistakes like mismatched quotes, missing semicolons, or Windows line endings in scripts.
Key Details
- One of the most common bash scripting errors
- The error message points to the token where bash got confused
- Common tokens: '(', ')', 'do', 'done', 'fi', 'then', newline
- Windows carriage returns (\r) are invisible but cause this error
- Missing or extra quotes, brackets, or keywords cause parsing failures
Common Causes
- Script created/edited on Windows containing \r\n line endings
- Missing 'then' after 'if' condition or 'do' after 'for/while'
- Mismatched or unclosed quotes (single or double)
- Missing semicolon before 'then', 'do', or closing keywords
- Copy-pasted code containing smart quotes or invisible Unicode characters
Steps
- 1Check for Windows line endings: cat -A script.sh (look for ^M at line ends)
- 2Convert line endings: dos2unix script.sh or sed -i 's/\r$//' script.sh
- 3Run bash syntax check without executing: bash -n script.sh
- 4Check for unmatched quotes: grep -n '"' script.sh | wc -l (should be even)
- 5Use shellcheck: shellcheck script.sh for comprehensive linting