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
Unix environments are able to execute many kinds of scripted programs directly via the '#!' magic header at the start of a plain text file. These bytes combined with execute permissions on the file cause the OS to look up and execute a separate program which is passed the script file as an argument - this results in execution of the script. This is used frequently with BASH shell scripts but also Python as well.
In many languages (Python, Bash etc) this functionality is enabled by treating the # character as a comment. C# does not treat # as a comment and so it is not currently possible to compile and execute a script directly as per the example below.
Aid Unix adoption by enabling drop in replacement for BASH/Python shell scripts
Detailed design
The pre-processor already supports a range of # directives C# Pre-processor directives. I suggest a new pre-processor directive '#!' be added that would function as a single line comment.
The pre-processor implementation would simply replace #! with //. It would also be recommended to make this directive only valid at the start of the file thereby mitigating the risk of breaking changes. As #! is not currently a preprocessor directive the risk of breaking changes is already minimal.
This proposal purposely doesn't define how the C# file will be compiled or run, this could either be:
Supplied as part of the runtime
Another script which performs the build and run
Something specified by the user
The snippets below give example syntax before and after the pre-processor has run
#!/usr/bin/env someunspecifiedprogram
using System;
namespace HelloWorld
{
class Hello {
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
///usr/bin/env someunspecifiedprogram
using System;
namespace HelloWorld
{
class Hello {
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Code changes required by accepting this feature as not particularly invasive as it is a similar case to the existing #define syntax (search SyntaxKind.DefineDirectiveTrivia for files that would likely need to be changed)
Drawbacks
As with any language syntax change this would potentially result in syntax errors in tools that were not #! aware. It is also not particularly useful to those running Windows environments.
Alternatives
None, #! must be the first two bytes of the file
Unresolved questions
Design meetings
The text was updated successfully, but these errors were encountered:
FEATURE_NAME
Summary
Unix environments are able to execute many kinds of scripted programs directly via the '#!' magic header at the start of a plain text file. These bytes combined with execute permissions on the file cause the OS to look up and execute a separate program which is passed the script file as an argument - this results in execution of the script. This is used frequently with BASH shell scripts but also Python as well.
In many languages (Python, Bash etc) this functionality is enabled by treating the # character as a comment. C# does not treat # as a comment and so it is not currently possible to compile and execute a script directly as per the example below.
Motivation
Detailed design
The pre-processor already supports a range of # directives C# Pre-processor directives. I suggest a new pre-processor directive '#!' be added that would function as a single line comment.
The pre-processor implementation would simply replace #! with //. It would also be recommended to make this directive only valid at the start of the file thereby mitigating the risk of breaking changes. As #! is not currently a preprocessor directive the risk of breaking changes is already minimal.
This proposal purposely doesn't define how the C# file will be compiled or run, this could either be:
The snippets below give example syntax before and after the pre-processor has run
Code changes required by accepting this feature as not particularly invasive as it is a similar case to the existing #define syntax (search SyntaxKind.DefineDirectiveTrivia for files that would likely need to be changed)
Drawbacks
As with any language syntax change this would potentially result in syntax errors in tools that were not #! aware. It is also not particularly useful to those running Windows environments.
Alternatives
None, #! must be the first two bytes of the file
Unresolved questions
Design meetings
The text was updated successfully, but these errors were encountered: