Run C# Scripts on macOS Like a Pro: A Guide to Executable C# Files

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:

ShellScript
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.

  1. Open a text editor and create a new file called HelloWorld.csx.
  2. At the top of the file, add the following line:
C#
   #!/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.

  1. Below the shebang, add the following code:
C#
   Console.WriteLine("Hello, World from an executable C# script!");

This code will print a message when we run the script.

  1. 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.

  1. Open a terminal and navigate to the directory where you saved the HelloWorld.csx file.
  2. Run the following command to make the file executable:
ShellScript
   chmod +x HelloWorld.csx

This command gives the script file the necessary permissions to be run directly as an executable.

  1. Now that it’s executable, you can run the script directly from the terminal by typing:
ShellScript
   ./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:

  1. Open a new file, or edit the existing one, and name it Calculator.csx.
  2. Add the shebang line at the top:
C#
   #!/usr/bin/env dotnet-script
  1. Write a short script that reads input, performs a calculation, and outputs the result:
C#
   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}.");
  1. Save the file, make it executable, and then run it:
ShellScript
   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.

Share this:

Leave a Reply