Project 2: Command Line Shell (v 1.0)

Starter repository on GitHub:

The outermost layer of the operating system is called the shell. In Unix-based systems, the shell is generally a command line interface. Most Linux distributions ship with bash as the default (there are several others: csh, ksh, sh, tcsh, zsh). In this project, we’ll be implementing a shell of our own.

You will need to come up with a name for your shell first. The only requirement is that the name ends in ‘sh’, which is tradition in the computing world. In the following examples, my shell is named crash (Cool Really Awesome Shell) because of its tendency to crash.

The Basics

Upon startup, your shell will print its prompt and wait for user input. Your shell should be able to run commands in both the current directory and those in the PATH environment variable (run echo $PATH to see the directories in your PATH). The execvp system call will do most of this for you. To run a command in the current directory, you’ll need to prefix it with ./ as usual. If a command isn’t found, print an error message:

[🙂]─[1]─[mmalensek@gamestop:~/P2-malensek]$ ./hello
Hello world!

[🙂]─[2]─[mmalensek@gamestop:~/P2-malensek]$ ls /usr
bin  include  lib  local  sbin  share  src

[🙂]─[3]─[mmalensek@gamestop:~/P2-malensek]$ echo hello there!
hello there!

[🙂]─[4]─[mmalensek@gamestop:~/P2-malensek]$ ./blah
crash: no such file or directory: ./blah

[🤮]─[5]─[mmalensek@gamestop:~/P2-malensek]$ cd /this/does/not/exist
chdir: no such file or directory: /this/does/not/exist



The shell prompt displays some helpful information. At a minimum, you must include:

In the example above, these are separated by dashes and brackets to make it a little easier to read. The process exit status is shown as an emoji: a smiling face for success (exit code 0) and a sick face for failure (any nonzero exit code or failure to execute the child process). For this assignment, you are allowed to invent your own prompt format as long as it has the elements listed above. You can use colors, unicode characters, etc. if you’d like. For instance, some shells highlight the next command in red text after a nonzero exit code.

You will format the current working directory as follows: if the CWD is the user’s home directory, then the entire path is replaced with ~. Subdirectories under the home directory are prefixed with ~; if I am in /home/mmalensek/test, the prompt will show ~/test. Here’s a test to make sure you’ve handled ~ correctly:

[🙂]─[6]─[mmalensek@gamestop:~]$ whoami

[🙂]─[7]─[mmalensek@gamestop:~]$ cd P2-malensek

# Create a directory with our full home directory in its path:
# **Must use the username outputted above from whoami)**
[🙂]─[8]─[mmalensek@gamestop:~/P2-malensek]$ mkdir -p /tmp/home/mmalensek/test

[🙂]─[9]─[mmalensek@gamestop:~/P2-malensek]$ cd /tmp/home/mmalensek/test

# Note that the FULL path is shown here (no ~):
[🙂]─[10]─[mmalensek@gamestop:/tmp/home/mmalensek/test]$ pwd


Your shell must support scripting mode to run the test cases. Scripting mode reads commands from standard input and executes them without showing the prompt.

cat <<EOM | ./crash
ls /
echo "hi"

# Which outputs (note how the prompt is not displayed):
bin  boot  dev  etc  home  lib  lost+found  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# Another option (assuming commands.txt contains shell commands):
./crash < commands.txt
(commands are executed line by line)

You should check and make sure you can run a large script with your shell. Note that the script should not have to end in exit.

To support scripting mode, you will need to determine whether stdin is connected to a terminal or not. If it’s not, then disable the prompt and proceed as usual. Here’s some sample code that does this with isatty:

#include <stdio.h>
#include <unistd.h>

int main(void) {

    if (isatty(STDIN_FILENO)) {
        printf("stdin is a TTY; entering interactive mode\n");
    } else {
        printf("data piped in on stdin; entering script mode\n");

    return 0;

Since the readline library we’re using for the shell UI is intended for interactive use, you will need to switch to a traditional input reading function such as getline when operating in scripting mode.

When implementing scripting mode, you will likely need to close stdin on the child process if your call to exec() fails. This prevents infinite loops.

Built-In Commands

Most shells have built-in commands, including cd and exit. Your shell must support:

Signal Handling

Your shell should gracefully handle the user pressing Ctrl+C:

[🙂]─[11]─[mmalensek@gamestop:~]$ hi there oh wait nevermind^C

[🙂]─[11]─[mmalensek@gamestop:~]$ ^C

[🙂]─[11]─[mmalensek@gamestop:~]$ ^C

[🙂]─[11]─[mmalensek@gamestop:~]$ sleep 100

[🤮]─[12]─[mmalensek@gamestop:~]$ sleep 5

The most important aspect of this is making sure ^C doesn’t terminate your shell. To make the output look like the example above, in your signal handler you can (1) print a newline character, (2) print the prompt only if no command is currently executing, and (3) fflush(stdout).


Here’s a demonstration of the history command:

[🙂]─[142]─[mmalensek@gamestop:~]$ history
  43 ls -l
  43 top
  44 echo "hi" # This prints out 'hi'

... (commands removed for brevity) ...

 140 ls /bin
 141 gcc -g crash.c
 142 history

In this demo, the user has entered 142 commands. Only the last 100 are kept, so the list starts at command 43. If the user enters a blank command, it should not be shown in the history or increment the command counter. Also note that the entire, original command line string is shown in the history – not a tokenized or modified string. You should store history commands exactly as they are entered (hint: use strdup to duplicate and store the command line string). The only exception to this rule is when the command is a history execution (bang) command, e.g., !42. In that case, determine the corresponding command line and place it in the history (this prevents loops).

I/O Redirection

Your shell must support file input/output redirection:

# Create/overwrite 'my_file.txt' and redirect the output of echo there:
[🙂]─[14]─[mmalensek@gamestop:~]$ echo "hello world!" > my_file.txt
[🙂]─[15]─[mmalensek@gamestop:~]$ cat my_file.txt
hello world!

# Append text with '>>':
[🙂]─[16]─[mmalensek@gamestop:~]$ echo "hello world!" >> my_file.txt
[🙂]─[17]─[mmalensek@gamestop:~]$ cat my_file.txt
hello world!
hello world!

# Let's sort the /etc/passwd file via input redirection:
[🙂]─[18]─[mmalensek@gamestop:~]$ sort < /etc/passwd > sorted_pwd.txt

# Order of < and > don't matter:
[🙂]─[19]─[mmalensek@gamestop:~]$ sort > sorted_pwd.txt < /etc/passwd

# Here's input redirection by itself (not redirecting to a file):
[🙂]─[20]─[mmalensek@gamestop:~]$ sort < sorted_pwd.txt
(sorted contents shown)

Use dup2 to achieve this; right before the newly-created child process calls execvp, you will open the appropriate files and set up redirection with dup2.

Background Jobs

If a command ends in &, then it should run in the background. In other words, don’t wait for the command to finish before prompting for the next command. If you enter jobs, your shell should print out a list of currently-running backgrounded processes (use the original command line as it was entered, including the & character). The status of background jobs is not shown in the prompt.

To implement this, you will need:

The difference between a background job and a regular job is simply whether or not a blocking call to waitpid() is performed. If you do a standard waitpid() with options = 0, then the job will run in the foreground and the shell won’t prompt for a new command until the child finishes (the usual case). Otherwise, the process will run and the shell will prompt for the next command without waiting.

NOTE: your shell prompt output may print in the wrong place when using background jobs. This is completely normal.

The readline library

We’re using the readline library to give our shell a basic “terminal UI.” Support for moving through the current command line with arrow keys, backspacing over portions of the command, and even basic file name autocompletion are all provided by the library. The details probably aren’t that important, but if you’re interested in learning more about readline its documentation is a good place to start.


Here’s some hints to guide your implementation:

Testing Your Code

Check your code against the provided test cases. You should make sure your code runs on your Arch Linux VM. We’ll have interactive grading for projects, where you will demonstrate program functionality and walk through your logic.

Submission: submit via GitHub by checking in your code before the project deadline.


Restrictions: you may use any standard C library functionality. Other than readline, external libraries are not allowed unless permission is granted in advance (including the GNU history library). Your shell may not call another shell (e.g., running commands via the system function or executing bash, sh, etc.). Do not use strtok to tokenize input. Your code must compile and run on your VM set up with Arch Linux as described in class. Failure to follow these guidelines will will result in a grade of 0.