2

I have some shell scripts written under windows. When under windows, I copy it from Notepad plus, paste it into Putty console, and they work perfectly. But they throw syntax errors if I run them in Linux.

I found that this error is caused by some special characters (M-oM-;M-?#!/bin/bash) preceding shebang. I cannot delete them. If I delete the whole shebang line, they move to the next line.

The weird thing is that only some of these scripts contain such characters but others are fine.

All these scripts were written using Notepad Plus.

Please note that neither the dos2unix command work nor deleting ^M characters at the end of line.

Thank you in advance!

devnull
  • 118,548
  • 33
  • 236
  • 227
JIA
  • 173
  • 4
  • 11
  • Can you use emacs under Linux to delete the special characters? or vi? – Digital Trauma Nov 16 '13 at 03:00
  • The M-oM-;M-? is being added as an encoding as you have indicated it is appearing on the first line no matter what that line contains. `cat -v` will show the characters on the Linux machine. The only real way to fix this is to open the files in normal notepad and save under a .txt file extension. Then open on Linux machine with emacs, vi, nano, etc. and save as a .sh extension. That should correct the issue. – MattSizzle Nov 16 '13 at 03:04
  • 3
    http://stackoverflow.com/questions/8432584/how-to-make-notepad-to-save-text-in-utf-8-without-bom has various suggestions. (The three meta characters are a UTF-8 BOM.) – rici Nov 16 '13 at 04:58
  • Thank you very much! problem solved after saving the scripts in UTF8 without BOM – JIA Nov 16 '13 at 08:11
  • From your notation, I am guessing you are using Emacs. You can `M-x find-file-literally` to revisit the file with encoding support disabled, then simply delete the first three bytes. – tripleee Nov 16 '13 at 13:33

0 Answers0