Entering the Echo Chamber

Create your GitHub repo for this lab here: https://classroom.github.com/a/myfRUx6k

The first command line utility we will study is called echo. What does echo do? Well, it echoes. Yep, that’s right:

[mmalensek@mmalensek-vm ~]$ echo

[mmalensek@mmalensek-vm ~]$

Hmmm?

[mmalensek@mmalensek-vm ~]$ echo Hello World!
Hello World!
[mmalensek@mmalensek-vm ~]$

Okay, yeah. You get it. This might not seem like a useful program, but it does come in handy when you want to print something to the terminal from a script – a bunch of terminal commands used to automate something. Or, even more useful, you can print environment variables (or really any shell variable) with it:

[mmalensek@mmalensek-vm ~]$ echo $HOME
/home/mmmalensek

[mmalensek@mmalensek-vm ~]$ echo $?
0

I can use it to find out my home directory, or the exit status of the last command. Neat!

What can echo do?

If we want to know exactly what echo is capable of, we can use the manual pages: run man echo to find out all about it. If you run the command on your Linux VM, you’ll notice that echo supports a LOT of different command line flags: things that set options that will effect how the program runs. For example, echo -n Hello World! does not print a newline character (\n) at the end of the string.

The BSD version of echo is a bit simpler:

NAME
     echo – write arguments to the standard output

SYNOPSIS
     echo [-n] [string ...]

DESCRIPTION
     The echo utility writes any specified operands, separated by single blank (‘ ’) characters and followed
     by a newline (‘\n’) character, to the standard output.

     The following option is available:

     -n    Do not print the trailing newline character.  This may also be achieved by appending ‘\c’ to the
           end of the string, as is done by iBCS2 compatible systems.  Note that this option as well as the
           effect of ‘\c’ are implementation-defined in IEEE Std 1003.1-2001 (“POSIX.1”) as amended by Cor.
           1-2002.  Applications aiming for maximum portability are strongly encouraged to use printf(1) to
           suppress the newline character.

     Some shells may provide a builtin echo command which is similar or identical to this utility.  Most
     notably, the builtin echo in sh(1) does not accept the -n option.  Consult the builtin(1) manual page.

EXIT STATUS
     The echo utility exits 0 on success, and >0 if an error occurs.

What we will build

In this lab assignment, you’ll build your own version of echo called sfecho. Because yours is from San Francisco, where everything’s better. Like Rice-A-Roni… and Folger’s Coffee.

Requirements

If you want to go the extra mile, implement the rule described in the man page above: if the last argument ends in \c then also disable the trailing newline.

Building your program

Make sure that your source file is called sfecho.c. To compile it, use:

cc -Wall sfecho.c -o sfecho

The -Wall flag tells the compiler to turn on all warnings.

Run with:

./sfecho Hello world
Hello world

Testing your program

Here are some test cases to verify that everything is working properly. You’ll notice that removing the newline at the end of the output makes the shell prompt display in a somewhat weird way – that’s normal.

[mmalensek@mmalensek-vm ~]$ ./sfecho Hello World!
Hello World!
[mmalensek@mmalensek-vm ~]$ ./sfecho -n Testing testing
Testing testing[mmalensek@mmalensek-vm ~]$ ./sfecho $SHELL
/bin/bash
[mmalensek@mmalensek-vm ~]$ # Arguments are separated by any amount of space,
[mmalensek@mmalensek-vm ~]$ # So our program won't see these:
[mmalensek@mmalensek-vm ~]$ ./sfecho This        is       a             TEST
This is a TEST
[mmalensek@mmalensek-vm ~]$ # Quotes will make this appear as a single argument:
[mmalensek@mmalensek-vm ~]$ ./sfecho 'Hello            World!'
Hello            World!
[mmalensek@mmalensek-vm ~]$ ./sfecho

[mmalensek@mmalensek-vm ~]$ ./sfecho -n all       done.
all done.[mmalensek@mmalensek-vm ~]$ 

When you are finished, check your code into your GitHub repository to submit.