Homework #2

Assigned: September 3
Due: September 10, 12:30pm


(50 points) Implement the Linux env utility in Go.

The env utility examines the environment and modifies it to execute another command. When called without arguments, the env command writes the current environment to standard output. The optional utility argument specifies the command to be executed under the modified environment. The optional -i argument means that env should ignore the environment inherited from the shell when executing utility. Without the -i option, env uses the [name=value] arguments to modify rather than replace the current environment to execute utility. The env command does not modify the environment of the shell that executes it. See the env manpage for more information.

      env [-i] [name=value] ... [utility [argument ...]]                                                 
POSIX: Shell and Utilities


Write a program that behaves in the same way as the env utility when executing another program.
  1. This assignment is asking you to implement env from scratch, not just call the system's installed version of env from a Go program.
  2. When called with no arguments, the env utility outputs the current environment to standard output.
  3. When env is called with the optional -i argument, the entire environment is replaced by the name=value pairs. Otherwise, the pairs modify or add to the current environment.
  4. If the utility argument is given, use exec.Command to execute utility after the environment has been appropriately changed. Otherwise, print the changed environment to standard output, one entry per line. Check the return value of exec.Command to handle any errors.
  5. Return a different integer as an exit status for an invalid option as that returned for an invalid utility. Mimic the behavior of env on our Linux system.
  6. Your program must be written in Go and compile without errors or warnings using go build on our system.
If designed properly, the program required to solve this homework should occupy no more than 100 lines of code.

How to submit

Note: All directory and filenames below are case-sensitive. You must use the directory and filenames exactly as shown below (i.e., all lower case).

Prepare your source code file as /home/<logname>/homeworks/hw2/env1.go, where <logname> is your login name (e.g., FA_15_CPS444_03). Only your source file env1.go will be electronically collected from this location in your Linux account on the deadline.

Failure to follow these submission requirements will result in a 10% penalty.


Ninety percent of your score will come from correctness and 10% of your score will come from following our programming style guide. Applicable submission penalties will then be applied.

Return Home