Skip to content

Commit 0e7112c

Browse files
shethaaditAdit Sheth
andauthored
Explain How Shebang Works with dotnet fsi in F# Scripting Section. (#44276)
* Resolved bug 38293. * Resolved comments. --------- Co-authored-by: Adit Sheth <[email protected]>
1 parent e904f43 commit 0e7112c

File tree

1 file changed

+36
-1
lines changed
  • docs/fsharp/tools/fsharp-interactive

1 file changed

+36
-1
lines changed

docs/fsharp/tools/fsharp-interactive/index.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The code's formatting is preserved, and there is a double semicolon (`;;`) termi
7070

7171
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.
7272

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`:
7474

7575
```fsharp
7676
let getOddSquares xs =
@@ -88,6 +88,41 @@ dotnet fsi Script.fsx
8888
[1; 9; 25; 49; 81]
8989
```
9090

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+
91126
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).
92127

93128
## Referencing packages in F# Interactive

0 commit comments

Comments
 (0)