Which shebang should I use for F# scripts in Mac OS X?
With the most recent crossplatform .net installed and the most recent linux (coreutil 8.30 <=) and bsd / mac os releases. You can use the following shebang for F# .fsx
scripts.
#!/usr/bin/env -S dotnet fsi
So, in @mcandre's answer, it works because most shells revert to bourne shell when they don't find a shebang.
So having #light
which is both valid F# and a comment in bourne shell allows each scripting environment to see what it understands.
The script inside however could be improved. When we execute a script adding --quiet
to --exec
seems redundant, also we expect all arguments to be passed to the script. A more ideal version would be exec fsharpi --exec $0 $*
so all arguments would be passed as if you ran the command explicitly.
#light
is not the only #
preceded directive in F#. Preprocessor defines also would work and could be intuitive to what is going on. Putting a shell script between #if run_with_bin_sh
and #endif
would be invisible to f# since run_with_bin_sh
wouldn't be defined.
Example fsx:
#if run_with_bin_sh
exec fsharpi --exec $0 $*
#endif
printfn "%A" fsi.CommandLineArgs
update: Real shebang support such as #!/usr/bin/env fsharpi --exec
has been add into the official Microsoft F# code base. So it should work with future versions of F#.
update 2: #!/usr/bin/env fsharpi --exec
works great for mac, but not on linux. Linux needs to be #!/usr/bin/fsharpi --exec
the incompatibilty is annoying.
If you want a cross platform fsharp shebang. The following will work.
#!/bin/sh
#if run_with_bin_sh
exec fsharpi --exec $0 $*
#endif
NOTE fsharpi
has been superseded by dotnet fsi
. See this answer.
In Mac OS X, the program is fsharpi
.
hello.fs:
#light (*
exec fsharpi --exec $0 --quiet
*)
System.Console.WriteLine "Hello World"