Search code examples
f#shebang

Which shebang should I use for F# scripts in Mac OS X?


I have F# 2.0 installed with Mono, and I'd like to ./ my F# scripts. Which shebang line should I use for Mac OS X? Can this shebang line be generalized for Mac OS X and Linux?


Solution

  • 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