Fix Windows Line Endings Unexpected Token Error for Bash Script

Unexpected token error $'{\r’

When you try to source or run your bash script and you get an error that looks like this

bash: syntax error near unexpected token `$'{\r''

Then you need to fix you windows appended line endings.

Fixing the unexpected token error

This will happen when you have tweaked the script in a windows environment. An immediate fix is to run the below sed script in the folder that has your problem bash script.

sed -i 's/\r//'

Long term consider using Git to check in your code and set it to automatically remove these line endings.

Leave a Reply

Your email address will not be published. Required fields are marked *