-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Explain How Shebang Works with dotnet fsi in F# Scripting Section. #44276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8bb759c
Resolved bug 38293.
211e4ae
Resolved comments.
8dbbcde
Apply suggestions from code review
BillWagner e42135e
Apply suggestions from code review
BillWagner 4d340f8
Update docs/fsharp/tools/fsharp-interactive/index.md
BillWagner 288f616
Update docs/fsharp/tools/fsharp-interactive/index.md
BillWagner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,7 +70,7 @@ | |
|
|
||
| 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. | ||
|
|
||
| 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`: | ||
| 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`: | ||
|
|
||
| ```fsharp | ||
| let getOddSquares xs = | ||
|
|
@@ -88,6 +88,41 @@ | |
| [1; 9; 25; 49; 81] | ||
| ``` | ||
|
|
||
| ### Executing Scripts with a Shebang | ||
|
|
||
| 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. | ||
|
|
||
| For example, create a script file called `ExecutableScript.fsx` with the following content: | ||
|
|
||
| ```fsharp | ||
| #!/usr/bin/env -S dotnet fsi | ||
| let getOddSquares xs = | ||
| xs | ||
| |> List.filter (fun x -> x % 2 <> 0) | ||
| |> List.map (fun x -> x * x) | ||
| printfn "%A" (getOddSquares [1..10]) | ||
| ``` | ||
|
|
||
| 1. **Make the Script Executable:** | ||
| Use the `chmod` command to make the script executable: | ||
|
|
||
|
Check failure on line 110 in docs/fsharp/tools/fsharp-interactive/index.md
|
||
BillWagner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ```bash | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| chmod +x ExecutableScript.fsx | ||
| ``` | ||
|
|
||
| 2. **Run the Script Directly:** | ||
| Now, you can execute the script directly from the terminal: | ||
|
|
||
| ```bash | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ./ExecutableScript.fsx | ||
| ``` | ||
|
|
||
| > **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. | ||
| This feature allows for a more seamless experience when working with F# scripts in environments like Linux and macOS. | ||
|
|
||
| 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). | ||
|
|
||
| ## Referencing packages in F# Interactive | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.