Using the command line "shell"

While you can write code in an IDE (like Visual Studio or Eclipse), for this course you should try to run your programs from the command line (or "shell"). The shell is a program that takes text input commands, and produces text output.

If you haven't used a shell before, it may seem old fashioned, obscure, and slow. That's just because you're not used to it. Once you learn it, you'll see that you can work much faster and more accurately than you could otherwise.

How to open a shell

The shell is probably already installed on the computer you're using. If not you can get it for free.

When you are on Windows the shell program that runs and interprets your commands is DOS. On Linux or OSX the shell is usually bash.

What directory am I in (what's my "working directory")?

When working in a shell, you are always in some directory. You can find out which one by doing the following:

The shell knows how to find some built-in commands, but to run other commands (like your own programs) you need to specify the directory, relative to where you currently are. So it's good to know where you are.

How do I move to another directory?

cd [directory name]

For example: cd project5 or cd project5/debug/tests (on bash) or cd project5\debug\tests (on DOS).

The parent directory is always .. (two periods), and the current directory is always . (one period). The character to divide the names of two directories is backslash (\) on DOS, forward slash (/) on bash.

Note that spaces in directory names can cause you problems -- because a space is how the shell determines the end of part of a command. So going to a directory with a space will require you to surround the directory name in quotes, e.g. cd "project 5/debug/test random", or escape the spaces with backslashes, e.g. cd project\ 5/debug/test\ random. Using tab-completion (see further down this page) can help do this automatically for you.

What is in the current directory?

Where are my files?

How do I compile from the shell?

You don't have to compile from the shell (you can use your IDE for this). But if you want to:

How do I run my program from the shell?

This is where the true power of the shell starts to appear.

File input and output redirection

When running a program from the shell, you need to provide it some input, and it will produce output. You might be tempted to just type in the input on the keyboard, and then look at the output. Those are wrong. Let me explain.

Typing input is errer errir error-prone. It also takes a long time.

Looking at output is also error-prone. You may not even be able see some problems, like an extra space at the end of a line.

Let's look at how to do this.

How to redirect input from a file

First, you need a file containing the input you want. It should be a plain-text file (e.g. you could create it with a text editor, such as Notepad on Windows, TextEdit on OSX, your chosen IDE, or a general-purpose text editor like VIM or Emacs). Let's assume the file is called "input".

Second, you run the program from the shell. After the program's name, put the text < input. So something like this:

programName < input        (on DOS)
./programName < input      (on bash)

What's going on here? The shell sees that you want to run a program. It also sees that you have <, which indicates input redirection. It uses the filename you provide, opens the file, and connects it to the standard input of the running program. Very easy to do!

How to redirect output to a file

Let's assume that you want to put the output in a file called "output". Run the program from the shell. After the program's name, put the text "> output". So something like this:

programName > output        (on DOS)
./programName > output      (on bash)

Now, you have exactly the text that the program produced. Why is this useful? See below, the section on diff.

Redirecting input and output

You can (and should) combine input and output redirection, e.g.:

programName < input > output        (on DOS)
./programName < input > output      (on bash)

Using diff

Diff (short for "difference") is a type of program that compares two files and shows their differences. Diff saves you time and simplifies your job of determining if two output files are the same. It will show you only the differences, allowing you to focus on just those.

In this class, you should use a diff program on your own computer to compare your output with the known-correct output (e.g. the output produced by a solution provided by your professor).

There are many programs that provide diff:

Most diffs allow stricter or looser comparisons. For example, you might specify whether differences in spacing are significant. For this course, you should select the strictest options.

Miscellaneous other things about using the shell


Copyright © 2014 Greg Hamerly.
Computer Science Department
Baylor University

valid html and css