I needed a way to run C# console programmes in my Mac Terminal, as I needed to load file input through stdin.

I learnt online that mono is the framework to do it:

Mono is a software platform designed to allow developers to easily create cross platform applications part of the .NET Foundation.

So I downloaded the VS release from here, and then hopped to the Mono Basics getting started guide to compile and run my first terminal C# program.

Adding the path to mono into bash PATH

As with Go I was told by terminal bash command not found.
I have been messing with PATH several times so I thought this should be the time that I figure out a robust way to add a new path and document it here.

This stackexchange post solved my problem. The steps I followed are:

  1. Find out where the bin for the new command is installed. For mono it is /Library/Frameworks/Mono.framework/Versions/Current/bin.

  2. open the ~/.bash_profile file

    1
    sudo nano ~/.bash_profile
  3. Here’s what the ~/.bash_profile file look like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
      GNU nano 2.0.6        File: /Users/anran/.bash_profile                        
    PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
    export PATH
    export PATH=/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bi$
    export PATH="$PATH:/usr/local/smlnj/bin"
    export PATH="$HOME/.composer/vendor/bin:$PATH"
    alias composer="php /usr/local/bin/composer.phar"
    export PATH="/usr/local/opt/php@7.3/bin:$PATH"
    export PATH="$PATH:/usr/local/smlnj/bin"
    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$

export explicitly changes the PATH env variable. Each export PATH="$PATH:<new_path> command adds a new path to PATH.

Add a new line just before the line to load RVM:

1
export PATH="$PATH:/Library/Frameworks/Mono.framework/Versions/Current/bin"

Save the file and exit.

  1. Reopen the terminal.

To verify the path is correctly added, run echo $PATH to see the new PATH‘s content.

  1. Now mono and csc are available directly through Terminal. To compile and execute my program:
1
2
3
4
5
6
$ csc Program.cs
Microsoft (R) Visual C# Compiler version 3.6.0-4.20224.5 (ec77c100)
Copyright (C) Microsoft Corporation. All rights reserved.

$ mono Program.exe
Hello World!