Search code examples
bashcygwinshebang

Prevent cygwin bash from logging error on shebang


When running bash scripts in cygwin from the Windows command prompt, I always get a error on line 1, even when the script runs correctly

C:\ImageMagickWatchFolder>bash .\whitebalance
.\whitebalance: line 1: #!/bin/bash: No such file or directory
... (rest of script output)

(whitebalance is the filename of the script)

I've tried replacing line 1 of the script with

  • #!/cygdrive/c/cygwin64/bash
  • #!bash
  • #!C:\cygwin64\bin\bash
  • #!C:\\cygwin64\\bin\\bash

among others, and just eliminating it entirely, all to no avail. (Some give No such file or directory and some give command not found). In particular, with first line empty, it gives

C:\ImageMagickWatchFolder>bash .\whitebalance
.\whitebalance: line 1: $'\357\273\277': command not found

I also tried switching line ending styles from unix to windows but that just made things worse.

To the first comment below:

C:\ImageMagickWatchFolder>head -n 2 whitebalance | cat -A
M-oM-;M-?#!/bin/bash$
coords=""$

Solution

  • Figured it out. I had been saving the script in Notepad++ in UTF-8-BOM encoding. When changed to UTF-8 or ANSI, the error no longer occurred.