Skip to content

Commit a8bbdb4

Browse files
authored
Fix the env shebang line in file-based app docs. (#52606)
`#!/usr/bin/env dotnet run` doesn't work because `env` treats "dotnet run" as a single command name. Also change the wording that suggests this is a shell/command-line feature. The executable files are also executable by other apps.
1 parent 7aa0eaa commit a8bbdb4

7 files changed

Lines changed: 23 additions & 17 deletions

File tree

docs/core/tools/dotnet-run.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,20 @@ To run the application, the `dotnet run` command resolves the dependencies of th
9191

9292
The path to the file-based app to run. If a path isn't specified, the current directory is used to find and run the file. For more information on file-based apps, see [Build file-based C# apps](../../csharp/fundamentals/tutorials/file-based-programs.md).
9393

94-
On Unix, you can run file-based apps directly, using the source file name on the command line instead of `dotnet run`. First, ensure the file has execute permissions. Then, add a shebang line `#!` as the first line of the file, for example:
95-
94+
On Unix, you can execute file-based apps directly using the filename. This requires a shebang line `#!` as the first line of the file and the *execute* permission to be set. For example:
95+
9696
```csharp
97-
#!/usr/bin/env dotnet run
97+
#!/usr/bin/env dotnet
9898
```
99-
100-
Then you can run the file directly from the command line:
101-
99+
100+
Set the execute bit:
101+
102+
```bash
103+
chmod +x ConsoleApp.cs
104+
```
105+
106+
Now you can run the file directly from the command line:
107+
102108
```bash
103109
./ConsoleApp.cs
104110
```

docs/csharp/fundamentals/program-structure/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Starting with C# 14 and .NET 10, *file-based apps* let you run a program contain
3737
:::code language="csharp" source="./snippets/file-based-program/hello-world.cs":::
3838

3939
> [!NOTE]
40-
> The `#!` line enables Unix shells to run the file directly. On any Unix system, set the *execute* (`+x`) permission and run the file from the command line.
40+
> The `#!` line enables Unix operating systems to execute the file directly (for example, `./hello-world.cs`). This requires the *execute* permission to be set (`chmod +x <file>`).
4141
4242
File-based apps support all C# syntax and can use [preprocessor directives](../../language-reference/preprocessor-directives.md#file-based-apps) to configure the build system. Use file-based apps for small command-line utilities, prototypes, and experiments. A file-based app consists of a single file in a directory:
4343

docs/csharp/fundamentals/program-structure/preprocessor-directives.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ C# preprocessor directives tell the compiler what code to include, exclude, or t
2424

2525
Starting with C# 14, [file-based apps](index.md) use two additional directives:
2626

27-
- `#!` — the *shebang* line that lets Unix shells run the file directly (for example, `#!/usr/bin/env dotnet run`).
27+
- `#!` — the *shebang* line that enables executing the file directly on Unix (for example, `./Program.cs`). This requires the *execute* permission to be set on the file (`chmod +x <file>`).
2828
- `#:` — build-system directives that configure packages, SDK settings, and other options for single-file programs.
2929

3030
Use `#:package` to add a NuGet package. For example, the following file-based app uses the `Spectre.Console` package to render styled output:
3131

3232
```csharp
33-
#!/usr/bin/env dotnet run
33+
#!/usr/bin/env dotnet
3434
#:package Spectre.Console@*
3535

3636
AnsiConsole.MarkupLine("[bold green]Hello[/] from a file-based app!");

docs/csharp/fundamentals/program-structure/top-level-statements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The compiler generates a method to serve as the program entry point for a projec
3939
| `return` | `static int Main(string[] args)` |
4040
| No `await` or `return` | `static void Main(string[] args)` |
4141

42-
Starting with C# 14, programs can be [*file-based apps*](./index.md#building-and-running-c-programs), where a single file contains the program. You run *file-based apps* by using the command `dotnet <file.cs>`, or by using the `#!/usr/bin/env dotnet` directive as the first line (Unix shells only).
42+
Starting with C# 14, programs can be [*file-based apps*](./index.md#building-and-running-c-programs), where a single file contains the program. You run *file-based apps* by using the command `dotnet <file.cs>`, or directly using the filename on Unix (for example, `./file.cs`). The latter requires including the `#!/usr/bin/env dotnet` directive as the first line and setting the execute permission (`chmod +x <file>`).
4343

4444
## `using` directives
4545

docs/csharp/fundamentals/tutorials/file-based-programs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ File-based apps are regular C# programs. The only limitation is that you must wr
6969
>
7070
> Support for `#!` directives applies on Unix platforms only. There's no similar directive for Windows to directly execute a C# program. On Windows, you must use `dotnet` on the command line.
7171
72-
On Unix, run file-based apps directly by typing just the source file name. Instead of using `dotnet AsciiArt.cs`, type the source file name on the command line. You need to make two changes:
72+
On Unix, you can execute file-based apps directly using just the source file name. You need to make two changes:
7373

7474
1. Set *execute* permissions on the source file:
7575

@@ -91,13 +91,13 @@ Alternatively, you can use `#!/usr/bin/env dotnet` to resolve the dotnet path fr
9191
#!/usr/bin/env dotnet
9292
```
9393

94-
After making these two changes, you can run the program from the command line directly:
94+
After making these two changes, you can run the program directly:
9595

9696
```bash
9797
./AsciiArt.cs
9898
```
9999

100-
If you prefer, you can remove the extension so you can type `./AsciiArt` instead. You can add the `#!` to your source file even if you use Windows. The Windows command line doesn't support `#!`, but the C# compiler allows that directive in file-based apps on all platforms.
100+
If you prefer, you can remove the extension so you can type `./AsciiArt` instead. You can add the `#!` to your source file even if you use Windows. Windows doesn't support `#!`, but the C# compiler allows that directive in file-based apps on all platforms.
101101

102102
## Read command line arguments
103103

docs/csharp/language-reference/preprocessor-directives.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ Although the compiler doesn't have a separate preprocessor, it processes the dir
5353

5454
The C# compiler ignores any preprocessor directive that starts with `#:` or `#!`.
5555

56-
The `#!` preprocessor directive enables Unix shells to directly execute a C# file by using `dotnet run`. For example:
56+
The `#!` preprocessor directive enables Unix operating systems to directly execute the C# file using its filename.
5757

5858
```csharp
59-
#!/usr/bin/env dotnet run
59+
#!/usr/bin/env dotnet
6060
Console.WriteLine("Hello");
6161
```
6262

63-
The preceding code snippet informs a Unix shell to execute the file by using `dotnet run`. The `/usr/bin/env` command locates the `dotnet` executable in your PATH, making this approach portable across different Unix and macOS distributions. The `#!` line must be the first line in the file, and the following tokens are the program to run. You need to enable the *execute* (`x`) permission on the C# file for that feature.
63+
The first line in the file informs a Unix operating system to execute the file using `dotnet`. The `/usr/bin/env` command locates the `dotnet` executable in your PATH, making this approach portable across different Unix and macOS distributions where `dotnet` may be installed at different locations. In addition to including this line in the C# file, you need to set the *execute* (`x`) permission (`chmod +x <file>`).
6464

6565
The `#:` directives that are used in file-based apps are described in the [file based apps reference](../../core/sdk/file-based-apps.md).
6666

docs/csharp/tour-of-csharp/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Beginning with C# 14 and .NET 10, you can create *file-based apps*, which simpli
4949

5050
:::code language="csharp" source="./snippets/file-based-programs/hello-world.cs":::
5151

52-
The first line of the program contains the `#!` sequence (shebang) for unix shells. The location of the `dotnet` CLI can vary on different distributions. On any unix system, if you set the *execute* (`+x`) permission on such a C# file that contains the shebang directive, you can run the C# file directly from the command line:
52+
The first line of the program contains the `#!` sequence (shebang) for unix operating systems. This enables executing the file directly using its name when the *execute* (`+x`) permission is set on the file. For example, you can run the C# file directly from the command line:
5353

5454
```bash
5555
./hello-world.cs

0 commit comments

Comments
 (0)