10.3 Simple Output: cat and echo

10.3.1 cat

cat(1) is short for “concatenate”. It was originally designed to merge text files into one, but can be used for many other purposes.

To merge two or more files into one, you simply list the files after the cat command and then redirect the new output to a file. cat works with standard input and standard output, so you have to use the shell redirection characters. For example:

% cat file1 file2 file3 > bigfile

This command takes the contents of file1, file2, and file3 and merges it all together. The new output is sent to standard out.

One can also use cat to display files. Many people cat text files through the more or less commands, like this:

% cat file1 | more

That will display the file1 file and pipe it through the more command so that you only get one screen at a time.

Another common use for cat is copying files. You can copy any file around with cat, like this:

% cat /bin/bash > ~/mybash

The /bin/bash program is copied to your home directory and named mybash.

cat has many uses and the ones discussed here are just a few. Since cat makes extensive use of standard input and standard output, it is ideal for use in shell scripts or part of other complex commands.

10.3.2 echo

The echo(1) command displays the specified text on the screen. You specify the string to display after the echo command. By default echo will display the string and print a newline character after it. You can pass the -n option to suppress the printing of the newline. The -e option will cause echo to search for escape characters in the string and execute them.