$ cat hello.ml
#!/usr/bin/env ocamlrun ocaml
let rec main = print_string "Hello World!\n"
$ ./hello.ml
Hello World!
$ ocaml hello.ml
Hello World!
$ ocamlc -o hello hello.ml
File "hello.ml", line 1, characters 0-1:
Error: Syntax error
$ ocamlopt -o hello hello.ml
File "hello.ml", line 1, characters 0-1:
Error: Syntax error
Similar to Erlang, OCaml permits shebangs in scripted mode, but borks in compiled mode. Is there a more idiomatic shebang in OCaml, one that doesn't trigger a syntax error during compilation?
Just use sed
as a preprocessor. To remove the shebang on the first line, if found, pass:
-pp 'sed "1 s/^#\!.*$//"'
to ocamlc
or ocamlopt
.