You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fsharp/tools/fsharp-interactive/index.md
+36-1Lines changed: 36 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ The code's formatting is preserved, and there is a double semicolon (`;;`) termi
70
70
71
71
Evaluating code interactively in F# Interactive can be a great learning tool, but you'll quickly find that it's not as productive as writing code in a normal editor. To support normal code editing, you can write F# scripts.
72
72
73
-
Scripts use the file extension **.fsx**. Instead of compiling source code and then later running the compiled assembly, you can just run **dotnet fsi** and specify the filename of the script of F# source code, and F# interactive reads the code and executes it in real time. For example, consider the following script called `Script.fsx`:
73
+
Scripts use the file extension **.fsx**. Instead of compiling source code and then later running the compiled assembly, you can just run **dotnet fsi** and specify the filename of the script, and F# Interactive reads the code and executes it in real time. For example, consider the following script called `Script.fsx`:
74
74
75
75
```fsharp
76
76
let getOddSquares xs =
@@ -88,6 +88,41 @@ dotnet fsi Script.fsx
88
88
[1; 9; 25; 49; 81]
89
89
```
90
90
91
+
### Executing Scripts with a Shebang
92
+
93
+
To make F# scripts executable without explicitly invoking `dotnet fsi`, you can use a shebang line at the top of the script. This enables you to run the script directly from the terminal, like a shell script.
94
+
95
+
For example, create a script file called `ExecutableScript.fsx` with the following content:
96
+
97
+
```fsharp
98
+
#!/usr/bin/env -S dotnet fsi
99
+
100
+
let getOddSquares xs =
101
+
xs
102
+
|> List.filter (fun x -> x % 2 <> 0)
103
+
|> List.map (fun x -> x * x)
104
+
105
+
printfn "%A" (getOddSquares [1..10])
106
+
```
107
+
108
+
1.**Make the Script Executable:**
109
+
Use the `chmod` command to make the script executable:
110
+
111
+
```bash
112
+
chmod +x ExecutableScript.fsx
113
+
```
114
+
115
+
2.**Run the Script Directly:**
116
+
Now, you can execute the script directly from the terminal:
117
+
118
+
```bash
119
+
./ExecutableScript.fsx
120
+
```
121
+
122
+
> **Note**: Shebang functionality (`#!`) is specific to Unix-like systems such as Linux and MacOS. On Windows, you can execute scripts using `dotnet fsi Script.fsx` directly in the terminal or command prompt.
123
+
124
+
This feature allows for a more seamless experience when working with F# scripts in environments like Linux and macOS.
125
+
91
126
F# scripting is natively supported in [Visual Studio](../../get-started/get-started-visual-studio.md) and [Visual Studio Code](../../get-started/get-started-vscode.md).
0 commit comments