Quick Fix: Run C# Code on Linux with Mono

Running C# (or C-Sharp) on Linux is pretty trivial unless you depend on something that's a Windows specific library. I just came a cross a tiny bash script (rather alias) I wrote when I was a teacher, since I also taught basic programming with C#. Since I'm cleaning up an old hard drive I stumbled across a small script I wrote for that.

Reading the code wasn't an issue, but I was running Linux (obviously) which may or may not have been sanctioned by the institution I was working for. I recall it wasn't specifically forbidden.

So what to do when a student approaches you or when you have to check their source code? Easy! Install Mono, run the code (that they wrote in Visual Studio Whatever).

Then, I created this little script and put it in my ~/scripts directory and called it csrun:

# build and run C# on linux

# get basename of file
fbname=$(basename "$1" .cs)
# compile and run with mono
gmcs $1 && mono $fbname.exe

My ~/scripts directory obviously was in my PATH:

Bottom line of my .bashrc:

export PATH=$PATH:/home/jonathan/scripts

Now I could run their source code easily or help them debug it when they sent it to me.

In the end it's a matter of convenience, because I didn't want to start the Mono IDE or whatever to check their code, but use my favourite editor and run it on the terminal.

Thank you for reading! If you have any comments, additions or questions, please tweet or toot them at me!