Coding⏱️ 3 min read📅 2026-06-11

How to Fix: "Syntax error near unexpected token '$'\r''" in shell script

Shell script syntax error due to unexpected token \r\r. Fix involves removing the \r\r from the command line arguments.

Quick Answer: Remove the \r\r from the command line arguments, e.g., func \\

The error "Syntax error near unexpected token '$' ''" in shell script" occurs when the shell interpreter encounters an unexpected character, such as a carriage return (` `), in the script. This error affects users who run the script directly from the terminal.

This error can be frustrating because it prevents the script from executing correctly. However, by following the steps outlined below, you should be able to identify and fix the issue.

🔍 Why This Happens

  • The main reason for this error is the presence of the carriage return (` `) character in the script. This character is not a valid shell syntax and causes the parser to malfunction.
  • Another possible cause is the use of single quotes (`'$'`) in the script, which can lead to unexpected token errors.

🔧 Proven Troubleshooting Steps

Identify and remove carriage return characters

  1. Step 1: Open the script file in a text editor and search for any occurrences of the carriage return (` `) character. Remove all instances of this character from the script.
  2. Step 2: Save the updated script file and try running it again to see if the error persists.
  3. Step 3: If the error still occurs, proceed to the next step.

Use double quotes instead of single quotes

  1. Step 1: Replace all instances of single quotes (`'$'`) with double quotes (`"$"`). This will ensure that the shell interprets the script correctly.
  2. Step 2: Save the updated script file and try running it again to see if the error persists.

🎯 Final Words

By following these steps, you should be able to identify and fix the "Syntax error near unexpected token '$' ''" in shell script" issue. Remember to always double-check your scripts for any unexpected characters or syntax errors.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions