How to Fix Bash Syntax Error Near Unexpected Token Newline in Linux

Executing a shell script or typing a chained pipeline into the Linux terminal only to receive bash: syntax error near unexpected token 'newline' halts script automation immediately. This parser error indicates that the GNU Bourne-Again Shell (Bash) encountered an end-of-line character where it strictly anticipated an argument, destination operand, or closing control token.

What Causes the “Unexpected Token Newline” Syntax Error in Bash?

Bash tokenises commands sequentially from left to right. Common mistakes that leave commands syntactically incomplete include:

  • Unfinished I/O Redirection: Leaving a redirection operator (such as >, >>, or <) at the end of a line without specifying a target file path.
  • Trailing Pipe or Logical Operators: Ending a line with |, &&, or || without escaping the line break with a backslash or continuing on the same line.
  • Windows CRLF Line Ending Pollution: Scripts edited on Windows containing carriage return characters (\r\n) that confuse Linux terminal interpreters.
  • Unquoted HTML or Special Characters: Passing unescaped ampersands or brackets inside command strings (such as URLs in curl or wget commands).

1. Identify and Correct Incomplete Redirection Operators

The single most frequent trigger is a redirection character with no destination specified.

# INCORRECT (Triggers syntax error near unexpected token 'newline'):
echo "Hello world" >

# CORRECT (Specifies destination file):
echo "Hello world" > output.txt

Review the exact line reported by the error. Ensure every >, >>, <, and 2> operator is followed by a valid target path or file descriptor (e.g. 2>&1).

2. Convert Windows (CRLF) Line Breaks to Unix (LF)

If the script was created or edited on Windows using Notepad or VS Code without setting line sequences to LF, hidden \r carriage returns corrupt shell parsing.

  1. Check for carriage returns using file or cat -v:
    file myscript.sh
    (If output states “with CRLF line terminators”, formatting must be cleaned).
  2. Convert the file using the dos2unix utility:
    sudo apt install dos2unix   # On Debian/Ubuntu
    dos2unix myscript.sh
  3. Alternatively, remove carriage returns using sed:
    sed -i 's/\r$//' myscript.sh

3. Escape Multi-Line Chained Commands Properly

When splitting long shell commands across multiple visual lines, every intermediate line must terminate with an explicit backslash escape character.

# INCORRECT (Bash expects next token immediately):
rsync -avz --progress \ 
/source/directory/ /backup/directory/

# NOTE: Ensure there are NO trailing spaces after the backslash:
rsync -avz --progress \
/source/directory/ /backup/directory/

A single accidental space after the trailing backslash invalidates the escape sequence, causing Bash to parse the backslash as a literal character and throw the unexpected newline error.

4. Quote URLs and Query Strings in curl and wget

Passing URLs with multiple parameters separated by unquoted ampersands causes Bash to treat & as a background job operator.

# INCORRECT:
curl https://api.example.com/data?user=admin&format=json

# CORRECT (Enclose URL in single quotes):
curl 'https://api.example.com/data?user=admin&format=json'

Enclosing query strings in single quotes prevents the shell from splitting tokens across subshells.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.