I'm trying to practice my F# by writing small console scripts in F# in place of my usual method of writing shell scripts/batch files. I currently run them using "fsi script.fsx [args]". Is there a way I can associate these fsx files with fsi so that I can run them directly like "script.fsx [args]"?
Yes it is possible.
There are two keys here:
You need to bind an application to execute to the ".fsx" extension. The exact way to do this may vary a bit between operating system versions (I have only done this in the past by editing the registry directly, so I can't provide a generic solution here); make sure the command to be executed does include the script arguments (as %*
), so the shell\open\command entry in the registry for the binding should look like
"c:\Program Files\FSharp-1.9.9.9\bin\fsi.exe" "@%1" %*
See Gradbot's response for more details.
As there are no answers so far, I'll try to turn my earlier comment into an answer with some more details.
I just tried to create a standard windows association for *.fsx files to open them automatically in fsi.exe and it works with no problems - when you open the fsx file, it runs F# Interactive and automatically evaluates the content of the file. This corresponds to running the command:
> fsi.exe test.fsx
However, I think that it is much easier to learn F# by using F# Interactive from Visual Studio. Even if you don't have a licence, you can use Visual Studio Shell with the free F# plugin. I think SharpDevelop also provides similar integration (but probably not with some limitations).
The easiest way to start is to create a new "F# Script" (File -> New -> File...) and then start writing some code (in an assorted order). Then you can select some part of the code and evaluate it in the F# Interactive window by perssing [Alt] + [Enter]
. You'll first need to open the F# Interactive window, which can be done using [Ctrl] + [Alt] + [F]
or you can find it in the View -> Other Windows menu item.
I think most of the F# webcasts use F# Interactive in this way, so if you look at some of them, you should get a good idea how to use it. Look for example at How Do I? videos at www.fsharp.net or F# videos at Channel 9.
I know this is very old but... Although I complete agree with everyone who suggests doing REPL-based development, if you find you've written a nice reusable script then here is what works well on Linux and MacOS. Put the *.fsx
script in your ~/bin folder or wherever and do the usual shebang trick that you do with other scripts:
First make your script
#!/usr/bin/env dotnet fsi
printfn $"1 + 1 = {1 + 1}"
make it executable and run it
chmod 755 foo.fsx
$ ./foo.fsx
1 + 1 = 2
Searching for "executing F# scripts on ubuntu" makes you land here.
To execute .fsx scripts with Mac/Linux, write fsharpi yourScript.fsx
, for windows users write: fsi yourScript.fsx
Here's a walk through on setting the association in windows 7.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With