Skip to main content Accessibility Feedback

Logging and printing things in Terminal

Today, we’re looking at how to log and print things in Terminal.

Note: I’m wrapping up production on a new series of courses on developer tooling. The first course on Terminal should be ready in the next week or two, but you can pre-order it today. This is an excerpt from the guide.

Printing things in Terminal

Use the echo command to print things in the Terminal window. Whatever you type after echo is printed.

# prints "Ahoy, matey"
echo Ahoy, matey

You can also wrap the text to log in quotes.

# also prints "Ahoy, matey"
echo "Ahoy, matey"

You can use \n to add a line break. This only works if your text is wrapped in quotes.

echo "Ahoy\nmatey"

Note: in Terminal, certain characters, like the bang operator (!) are used with commands. If included in an echo string, they can have unintended side-effects unless you escape them with a backslash (\) like this: echo "Ahoy, matey\!".

Reading the contents of a file

Use the cat command to read the contents of a file and print it into the console. Use the filename as an argument.

# prints the contents of the file

Here, the full contents of the file are printed into the Terminal window.


If you use the -n option before your filename, line numbers are included.

# print the file with line numbers
cat -n

File names can be wrapped in quotes. You can also pass in multiple files as arguments.

# print the contents of two files
cat "" ""

# print multiple files with line numbers
cat -n "" ""

Sorting the contents of a file

Use the sort command to sort the contents of a file alphabetically, in reverse order, by number, or by month. It can also remove duplicates.

By default, spaces are treated as the delimiter. The original file is not modified.

For example, let’s imagine that we have a file that looks like this. The character types are not in any sort of order.


To sort them alphabetically, we would do this.

# sort the contents of the file alphabetically

This is what’s printed to the console as a result.


You can sort in reverse order using the -r option.

# sort the contents in reverse alphabetical order
sort -r

To sort by numeric order, use the -n option. You can also sort in reverse-numeric order using -n and -r together.

For example, imagine that you have a file that tracks an RPG characters health.

12 - Wizard
11 - Druid
20 - Knight
4 - Bard

To sort it numerically (in ascending or reverse order, respectively), you would do this.

# sort in numeric order
sort -n

# sort in reverse numeric order
sort -nr

To remove duplicates while sorting, use the -u option.

For example, imagine you have a file with some duplicates in it.


To sort them and remove the duplicates, you would do this.

# remove duplicates
sort -u

You can use the -o option to save the sorted output to a file. Add it to the end of your command, other options, and argument, with the filename as an option value.

# sort alphabetically and save to a new file
sort -u -o

Clearing the Terminal window

The clear command will remove all content from the Terminal window.

# clear the Terminal window