18.3. Command Line Introduction

Sometimes we will be directing you to use a command window or terminal to compile and run C# programs. [1]

Reasons to use the command line:

  • The command line precedes the graphical user interface (GUI) used in modern operating systems and provides a simpler interface for input and output that is very flexible and powerful for knowledgeable users.
    • Input comes from the keyboard as typed characters (no mouse processing). Commands are only processed once you press Enter.
    • Output goes to the monitor as textual information (no window processing).
    • In C# these input/output mechanisms are called Console processing.
    • Input from and output to files is done in a very similar way, simplifying learning.
  • Many software development organizations use command line processing to automate creating, compiling (“building”), and running or executing software programs.
    • Command line “scripts” can be created to automate routine tasks.
    • Command line scripts are similar to C# and other computer programs.
    • Serious software developers should be familiar with the command line.

The most direct way to access the command line (often called a command shell):

  • On Windows, to have easy access to Mono tools, press the Windows key (lower left or lower right on the keyboard) and type Mono, then select the Mono Command Prompt and press Enter.

    Alternately, if you do not need Mono tools for sure, the general way to get a command window is to press the Windows key and R (lower or upper case) together, then type cmd and press the Enter or Return key – this brings up the basic command processing program, cmd.

  • On a Mac just open a Terminal window – this is fine for the Mono SDK commands.

Mac OSX, Linux and other Unix variants work basically the same once you get to a terminal, so we will only distinguish Windows and Mac OS-X.

18.3.2. Common Commands

The command shell waits for you to type in a command (a short name that the shell recognizes) followed by 0 or more parameters separated by spaces (and Enter). Note that if a parameter contains spaces you must surround the parameter value with matching single or double quotes – you’ll see an example later.

We are going to mention some of the simplest uses of basic commands. More advanced documentation would include more options.

Some commands are common between the Windows and Mac shells:

dir (Windows) or ls (Mac)
to list all the files a in the current directory or a named directory.
cd

stands for Change Directory – you can use this command to change the current working directory to a different one.

You can use this command to change to directories where your C# program source files are located, if different from the initial directory.

On Windows, suppose you created a directory C:\COMP170\hello; to change to that, type cd C:\COMP170\hello and press Enter – the shell prompt will change to show this new directory location and programs like mcs and mono will be able to access files there, directly by name. If the Comp170 directory was you current directory, it would be shorter to use relative paths and just cd hello. Remember if you want a different Windows drive, you must first use a drive change command.

On a Mac absolute or relative paths work with cd. There is no issue with drives.

If you included a space in one or more of the directory names, for example C:\COMP 170\hello (a space between COMP and 170) you should enclose the path in quotes like: cd "C:\\COMP 170\\hello"

Mac Note: if you type just cd and press Enter you will change back to your home directory. There is also a shorthand name for your home directory in command paths: tilde (~), often shifted backquote on the keyboard. Sorry, no such thing with Windows.

mkdir

stands for make directory – you can use mkdir to create a new empty directory in the current directory.

For example, on a Mac with current directory /Users/YourLogin, type mkdir hello and press Enter – this will create a new directory /Users/yourLogin/hello if it did not exist before; you can now create a C# source file in that directory and enter cd hello in the command shell.

An optional Windows abbreviation is md.

rmdir

removes an empty directory that you give as parameter, e.g.,

rmdir hello

With Mono installed (and for Windows, with a Mono command window), the programs associated with Mono can be used:

mcs
compiles one or more listed C# source files without using Xamarin Studio.
csharp
is the interactive C# statement testing program.

Other useful commands, with different names for Windows and Mac, are listed next by generic function, with general Windows syntax first and Mac second, and then often examples in the same order:

Display the contents of a text file in the command window. The Unix/Mac name origin: a more complicated use of cat is to concatenate files.

type textFileName
cat textFileName
type my_program.cs
cat my_program.cs

Make a copy of a file. Caution: If the second file already exists, you wipe out the original contents!

copy originalFile copyName
cp originalFile copyName
copy prog.cs prog_bak.cs
cp prog.cs prog_bak.cs

Erase or remove a file:

erase fileToKill
rm fileToKill
erase poorAttempt.cs
rm poorAttempt.cs

Another Windows equivalent is del (short for delete).

We have explained the simplest use of many of the commands above. Many modifiers are possible.

Help on a command:

help commandName;
commandName –help

Note the double dash above: This sometimes works for concise help on a Mac while you can generally get an immensely detailed help overload on a Mac from

man commandName

18.3.3. Scripts

This is not a subject of this course, but commands can be combined into script files.

Scripting languages are in fact whole new specialized programming languages, that include many of the types of programming statements found in C#.

18.3.4. Copy and Paste

Copying or pasting with a Mac is is the same with a terminal as in other editing: Use the same Apple Command key with C or P, and you can select with the mouse.

In Windows it is more complicated when using a command window: You can paste into the current command line by right clicking on the Command Window Title bar, and select edit and then paste.

By default a Windows command window is not sensitive to the mouse. You can change so that it is sensitive for select and copy: Right click in the title bar, select defaults, and make sure the check boxes under edit options are all checked. (The last two are explained in the next section.) Click OK. Then you can select with mouse and press Enter for the selection to be remembered in the copy buffer.

18.3.5. Command Line Shortcuts

Both Mac and Windows (with the right options selected, like the Windows check boxes in the last section), allow you to reduce typing:

You can bring back a previous command for the history of commands that are automatically remembered: Use the up and down arrows. This makes it very easy to run the same command again, or to make slight edits.

Both Windows and OS-X can see what files are in any directory being referred to. If you just start to type a file or folder name and then press the Tab key, both Windows and OS-X will do file completion to complete the name if there is no ambiguity. If there is ambiguity, they work differently:

  • Windows will cycle through all the options as you keep pressing Tab.
  • On the first tab OS-X will do nothing but give a sound if there is ambiguity, but the second tab will list all the options. Then you need to type enough more to disambiguate the meaning.
[1]Thanks to Dr. Robert Yacobellis for elaborations to this section.