From Coder Merlin
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Echo to the Screen[edit]

The echo command echoes the argument to the output. Let's try it. Type: echo "Hello, World!" and press RETURN. The echo command dutifully echoes the argument to the screen. Try it now:

john-williams@codermerlin:~/Merlin/hello$  echo "Hello, World!"

Hello, World! 

Create an Empty File[edit]

The touch command creates a new, empty file, if such a file does not already exist. If such a file does exist, the access and modification times are set to the current time. Try it now:

john-williams@codermerlin:~/Merlin/hello$  touch newFile.txt

john-williams@codermerlin:~/Merlin/hello$  ls


We'll see the cat command more below, but for now it's enough to understand that it displays the contents of a file. Because this is a new, empty file, nothing is displayed. Try it now:

john-williams@codermerlin:~/Merlin/hello$  cat newFile.txt



Remember that we said that almost everything in Unix is either a file or a process and that the screen could be considered as a file. We can redirect the output from echo to another file, one that we'll store in the file system. We do this with the > (greater-than) symbol. Normally, commands send their output to a device called stdout. This is what the echo command did when we used it earlier. In our case, stdout is printed to our terminal. Let's try to redirect our output to a file. Type: echo "Hello, World!" > out.txt and press RETURN:

john-williams@codermerlin:~/Merlin/hello$  echo "Hello, World!" > out.txt

Observe, Ponder, and Journal: : Section 6
  1. Was "Hello, World!" printed to the screen as before? If not, why not? What do you think happened to the text?

No output was displayed; we just received a new prompt. Let’s use the ls command to take a look:

john-williams@codermerlin:~/Merlin/hello$  ls

newFile.txt out.txt 

We can see that the directory contains one file, the "out.txt" that we created with the echo command. How can we see what's in this file?

Quickly View File Contents[edit]

To quickly view the contents of a file, we can use the cat command. The cat command concatenates files together and then sends the output to the stdout (standard output). (Remember that in our case, this is the screen.) Let's try it. Type "cat out.txt" and press RETURN.

john-williams@codermerlin:~/Merlin/hello$  cat out.txt

Hello, World! 

Append a Small Amount of Text to an Existing File[edit]

If we already have an existing file with some text, we can append (add text to the end) of this file by using echo and redirection as before, but with two "greater-than" symbols next to each another. Try it now. Type: echo "Hello, again!" >> out.txt

john-williams@codermerlin:~/Merlin/hello$  echo "Hello, again!" >> out.txt


Let's move back up to our home directory. Remember that we can do this by typing "cd" without any arguments.

john-williams@codermerlin:~/Merlin/hello$  cd