C# is known for its robustness and versatility, but sometimes it feels heavy for quick scripts or utility tasks. In cases like this, you don’t need a full project setup. Instead, you can use C# scripts that run as single files, much like a Bash or Python script. When working on macOS, making C# scripts directly executable is not only possible but also surprisingly easy. In this guide, I’ll walk you through how to turn a C# script into an executable file on macOS, including setting up a shebang and making the script work with minimal setup.
Thank me by sharing on Twitter 🙏
By the end of this post, you’ll have a powerful, lightweight approach to running C# code quickly on your Mac without needing a full IDE or project structure.
Why Use C# Scripts on macOS?
Before diving into the steps, it’s worth asking: why use C# in this way on macOS? C# is traditionally seen as part of the Windows development ecosystem, but with .NET Core, it became fully cross-platform, opening up new possibilities for C# developers on macOS and Linux. By using single-file C# scripts, you get the power of C# without the overhead of a full .NET project. It’s ideal for automation tasks, quick utilities, and small prototyping efforts.
Step 1: Setting Up dotnet-script
on macOS
To make C# scripts executable on macOS, we’ll need the dotnet-script
tool. This tool enables us to run .csx
files (C# script files) directly, and it’s compatible across different operating systems.
To install dotnet-script
, make sure you have the .NET SDK installed. You can get the SDK from the .NET website. Once that’s done, open up a terminal and install dotnet-script
globally with:
The Legend of Zelda Encyclopedia
$20.00 (as of December 21, 2024 19:39 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)USB Type-C to A Cable 5pack 6ft Braided Fast Charging 3A Quick Charger Cord, 6 Foot Compatible iPhone 16/15,Samsung Galaxy S10 S9 S8 Plus, Note 10 9 8, LG V50 V40 G8 G7(Grey)
$8.54 (as of December 21, 2024 08:38 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Nexus: A Brief History of Information Networks from the Stone Age to AI
$22.45 (as of December 21, 2024 19:39 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)dotnet tool install -g dotnet-script
After running this command, dotnet-script
will be available in your terminal, ready to run C# scripts.
Step 2: Writing a C# Script with a Shebang
Now that we have dotnet-script
set up, it’s time to write our C# script. A shebang is a line at the top of a script that tells the operating system what interpreter to use when running it. In our case, we’ll use dotnet-script
as the interpreter.
- Open a text editor and create a new file called
HelloWorld.csx
. - At the top of the file, add the following line:
#!/usr/bin/env dotnet-script
This line tells macOS to use dotnet-script
to interpret the file. The /usr/bin/env
part is a common pattern in shebang lines that finds the interpreter in your PATH
, so it doesn’t require the absolute path to dotnet-script
.
- Below the shebang, add the following code:
Console.WriteLine("Hello, World from an executable C# script!");
This code will print a message when we run the script.
- Save the file with the
.csx
extension..csx
stands for C# script, and it’s the standard extension for single-file C# scripts.
Step 3: Making the Script Executable
At this point, we have a C# script with a shebang at the top. But to make it runnable directly, we still need to give it executable permissions. This step ensures that macOS recognizes the file as an executable script.
- Open a terminal and navigate to the directory where you saved the
HelloWorld.csx
file. - Run the following command to make the file executable:
chmod +x HelloWorld.csx
This command gives the script file the necessary permissions to be run directly as an executable.
- Now that it’s executable, you can run the script directly from the terminal by typing:
./HelloWorld.csx
You should see the message “Hello, World from an executable C# script!” printed to the terminal.
Step 4: Enhancing the Script with User Input
Now that you have a basic C# script working, let’s explore the potential of scripting by adding a bit of interactivity. For instance, we could create a script that takes user input and performs a calculation. Here’s an example of a calculator script that adds two numbers:
- Open a new file, or edit the existing one, and name it
Calculator.csx
. - Add the shebang line at the top:
#!/usr/bin/env dotnet-script
- Write a short script that reads input, performs a calculation, and outputs the result:
Console.Write("Enter the first number: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter the second number: ");
int num2 = int.Parse(Console.ReadLine());
int sum = num1 + num2;
Console.WriteLine($"The sum of {num1} and {num2} is {sum}.");
- Save the file, make it executable, and then run it:
chmod +x Calculator.csx
./Calculator.csx
When you run this script, it will prompt you to enter two numbers and then output their sum.
Step 5: Automating and Integrating C# Scripts
One of the benefits of making C# scripts executable is that you can integrate them into broader workflows, like automated tasks or shell scripts. For example, you might create scripts to automate parts of your development environment, like managing local databases or testing APIs.
With an executable C# script, you can:
- Call it from other scripts, like Bash or Python.
- Schedule it using
cron
for routine tasks. - Share it with teammates, who can use it without needing to open an IDE or compile code.
Conclusion
Using executable C# scripts on macOS is a powerful way to streamline tasks, prototype quickly, and make use of C# without the overhead of creating a full .NET project. By combining a shebang, dotnet-script
, and a few simple terminal commands, you can create robust C# utilities that are easy to manage and execute on the fly.
The best part? This approach is cross-platform. While our example is specific to macOS, you can apply the same techniques on Linux, and with slight modifications, even on Windows.