Skip to content

Commit 8bb759c

Browse files
author
Adit Sheth
committed
Resolved bug 38293.
1 parent 68a0940 commit 8bb759c

File tree

1 file changed

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

1 file changed

+32
-1
lines changed

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

Lines changed: 32 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,37 @@ 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+
```bash
111+
chmod +x ExecutableScript.fsx
112+
```
113+
114+
2. **Run the Script Directly:**
115+
Now, you can execute the script directly from the terminal:
116+
```bash
117+
./ExecutableScript.fsx
118+
```
119+
120+
This feature allows for a more seamless experience when working with F# scripts in environments like Linux and macOS.
121+
91122
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).
92123

93124
## Referencing packages in F# Interactive

0 commit comments

Comments
 (0)