Harley Hahn
Home Page

Send a Message
to Harley

A Personal Note
from Harley Hahn

Unix Book
Home Page

List of Chapters

Table of Contents

List of Figures

   1   2   3
   4   5   6
   7   8   9
  10  11  12
  13  14  15
  16  17  18
  19  20  21
  22  23  24
  25  26


  A  B  C
  D  E  F
  G  H


• Alphabetical
• By category



Errors and



Home Page
& Overview

& Answers

The Unix Model
Curriculum &
Course Outlines

PowerPoint Files
for Teachers

Chapter 8...

Programs to Use Right Away

When you enter a command at the shell prompt, you are actually telling the shell to run the program by that name. For example, when you enter the date command, you are asking the shell to run the date program(*).

* Footnote

Strictly speaking, this is not the entire story. Some commands are "built into" the shell, which means they are not actually separate programs.

At this point, however, the distinction between separate programs and built-in programs is not important.

Unix has literally thousands of different programs, which means there are thousands of different commands you can enter. Many of these programs require that you understand some theory. For instance, before you can use the file system commands you need to learn about the file system. Other programs are so complex as to require a great deal of time to master. This is the case, for example, with the two principal Unix text editors, vi and Emacs. These are very useful programs, but extremely complicated. In fact, in this book, I devote an entire chapter to vi.

There are, however, programs that require no special knowledge and are not especially difficult to use. These are the programs that you can use right away and, in this chapter, we'll take a look at some of them.

Of the many, many Unix programs, I have chosen a few that I feel are particularly useful, interesting, or fun. I have two goals. First, I want you to know about the programs, as they really are useful, interesting or fun. Second, after talking so much about general principles earlier in the book, I want to give you a chance to get used to the Unix CLI (command line interface). Traditionally, one of the ways in which people learn Unix is by having a good time while they are learning. Let's, you and I, carry on that tradition.

Jump to top of page

Finding a Program on Your System:
which, type, whence

As we discussed in Chapter 2, Unix is not a specific operating system. It is a family of operating systems: Linux, FreeBSD, Solaris, AIX, and on and on. Moreover, even Linux itself is not one system. The name "Linux" refers to any Unix system that uses the Linux kernel, and there are literally hundreds of different Linux distributions.

Although the various Unixes have a lot in common, they don't all come with the exact same set of programs. For example, there are programs you will find in the Berkeley Unixes (FreeBSD, NetBSD and OpenBSD) that you won't find in Linux systems. Moreover, even when you compare two computers that are running the same version of Unix, you may find different programs. This is because, during a Unix installation, there is a lot of choice as to which programs should be installed, and not everyone makes the same choices.

It is true that basic Unix is basic Unix, and most systems have all of the important programs. Still, no matter what type of Unix you are using, there may be programs in this chapter (or elsewhere in the book) that are not available on your system.

For this reason, there are two important questions I would like to consider now: How do you know if a particular program is available on your system? If it isn't, is there anything you can do about it?

The easiest way to see if you have access to a program is to type its name at the shell prompt. (Remember, the job of the shell is to interpret your commands.) If the program exists on your system, something will happen. If not, the shell will tell you that it can't find that program. Don't worry about experimenting in this way. If you enter a non-existent command, it won't cause a problem. All that will happen is that you will see an error message.

A more precise way to check if a program is available is to use the which command. The purpose of which is to have the shell answer the question: If I were to enter a specific command, which program would be run? If there is an answer to this question, the program is installed on your system and you can use the command. If not, the command is not available.

To use the which command, follow it with the names of one or more programs, for example:

which date
which date less vi emacs

Here is the output for the first command:


In this case, which is telling you that, if you were to enter the date command, the shell would run the program stored in the file /bin/date. If you don't understand the Unix file system (which we will discuss in Chapter 23), this name will not make sense to you just yet. Don't worry about it. The important thing is that which has found a program to run, which tells you that date is a valid command on your system.

What happens if you ask which about a program that does not exist? For example:

which harley

There are two possibilities depending on your version of which. First, nothing may happen. That is, there will be no output. This means which could not find the program you specified. This is a characteristic of many Unix programs: if they don't have anything to say, they don't say it. (If only more people had the same philosophy.)

The second possibility is that you will see an error message. Here is a typical one:

/usr/bin/which: no harley in (/usr/local/bin:/usr/bin:

What which is telling you is that it can't find a program named harley anywhere in your search path. We'll talk about search paths when we discuss the various shells. For now, the important thing is to realize that, because which cannot find a program named harley, you can't use the harley command on your system.

So what do you do if you want to try one of the programs in this chapter, and it doesn't seem to be on your system? As obvious as it might seem, start by checking your spelling. Smart people make spelling mistakes all the time, and you and I are no exception(*).

* Footnote

In fact, seeing as you and I are smarter than most other people, we probably make more spelling mistakes because our minds work so quickly.

If you have spelled the command correctly and it just can't be found, you have several choices. First, you can forget about it. This chapter has a variety of programs to try and most of them will be available on your system.

Second, if you have access to other Unix systems, you can try them to see if they have the command.

Third, if you are using a shared system, you can ask the system manager if he or she could please install the program on your system. System managers are very busy people but, for a knowledgeable person, it doesn't take long to install a new program, so if you are polite and charming you may get what you want.

Finally, if you are using your own Unix computer, you can install the program for yourself. The details of how to do so vary with one type of Unix to another and are, alas, beyond the scope of this chapter. Perhaps you can find a local nerd who might be able to help you with your particular system.

If you use Bash for your shell (see Chapter 11), there is an alternative to the which command, type, for example:

type date

If you use the Korn shell (also Chapter 11), you can use the whence command:

whence date

The type and whence commands will sometimes display more detailed information than which. This can be useful in certain circumstances. However, for practical purposes, which will be fine most of the time.

Before we leave this section let me ask you, what do you think would happen if you use the which command on itself. In other words, which which is which? Give it a try:

which which

If you use Bash, try:

type which
which type
type type

Jump to top of page

How Do You Stop a Program?

Most Unix commands carry out one task and then stop automatically. For example, in the next section, we will meet the date command. All it does is display the time and date. It then stops and returns you to the shell prompt.

Other commands are more complex. When you start them, they put you in an environment in which you interact with the program itself by entering one command after another. When you are finished working with the program, you enter a special command to quit the program, at which point the program stops and you are returned to the shell prompt.

An example of this type of command is bc, a calculator program we will discuss later in the chapter. When you use bc, you start it and then enter one calculation at a time for as long as you want. When you are finished, you must tell bc that you are ready to quit. To do so, you enter the quit command .

With such programs, there will always be a special command that you can use to quit the program. The command will often be q or quit. To find out for sure, you can read the documentation for the program in the online Unix manual (which we will discuss in Chapter 9).

Aside from a special quit command, you can often stop a program simply by telling it there is no more data. To do so, press ^D (<Ctrl-D>), the eof key (see Chapter 7). This is often the easiest way to stop a program. For example, when you are using bc, just enter ^D and the program stops.

If all else fails, you may be able to stop a program by pressing the intr key, which will be either ^C or <Delete> (again, see Chapter 7).

— hint —

Most programs that have their own interactive environment come with some type of built-in help. To access the help facility, try typing help or h or ? (a question mark).

Jump to top of page

Displaying the Time and Date: date

The date command is one of the most useful of all the Unix commands. Simply enter:


and Unix will display the current time and date. Here is some sample output. Notice that Unix uses a 24-hour clock.

Sun Dec 21 10:45:54 PST 2008

If you live in a place that uses daylight savings time, Unix knows how to spring forward and fall back at the appropriate times. The example here shows Pacific Standard Time. In the summer, it would be Pacific Daylight Time.

Notice that date gives both the time and date, so this is the command to use when you want to know what time it is. There is a time command, but it does not display the time. It measures how long it takes to run a program.

Internally, Unix does not really run on local time. All Unix systems use Coordinated Universal Time (UTC), which is the modern name for Greenwich Mean Time (GMT). Unix silently converts between UTC and your local time zone as necessary. The details about your local time zone are specified at the time Unix is installed.

Sometimes it is handy to see what time it is in UTC. To display the time in UTC, enter:

date -u

You will see a time and date like this:

Sun Dec 21 18:45:54 UTC 2008

This time, by the way, is the UTC equivalent of the time in the previous example.

-- Harley ormation, see Appendix F, in which I explain all about time zones, 24-hour time, and UTC. I also show you how to convert from one time zone to another, illustrating it all with a few practical examples.

Jump to top of page

Displaying a Calendar: cal

One of the nice things about Unix is that it was not designed by a committee. When a Unix programmer decided he wanted a new tool, he could write it himself and add it to the system. A good example of this is the cal command, which displays a calendar.

To display the calendar for the current month, enter:


To display a calendar for a particular year, just specify the year. For example:

cal 1952

When you specify a year, be sure to type all four numbers. For example, if you enter cal 52, you will get the calendar for 52 A.D. If you want 1952, you need to use cal 1952. You can use any year between 1 and 9999.

— hint —

When you display the calendar for a full year, the output is so long that it may not fit on your screen.

If the top part of the calendar scrolls out of sight before you get a chance to read it, you can display the output one screenful at a time, by sending it to the less program. For example:

cal 1952 | less

(We will talk about less in Chapter 21.)

To display a calendar for a particular month, specify that month as a number between 1 and 12 (1 = January), as well as the year. For example, to display the calendar for December 1952, enter:

cal 12 1952

You will see:

    December 1952
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

If you want a specific month, you must always specify both the month and the year. For instance, if you want a calendar for July 2009, you must enter:

cal 7 2009

If you enter:

cal 7

you will get a calendar for the year for 7 A.D.

If, instead of displaying dates, you want to see the numbers of the days of the year, from 1 to 365 (Jan 1 = 1; Jan 2 = 2; and so on), the cal program can oblige. Just type -j after the name date. Here is an example.

cal -j 12 2009

The output is:

       December 2009
Sun Mon Tue Wed Thu Fri Sat
        335 336 337 338 339
340 341 342 343 344 345 346
347 348 349 350 351 352 353
354 355 356 357 358 359 360
361 362 363 364 365

Suppose you want to figure out whether or not a particular year is a leap year. A non-Unix person would check if the year is divisible by 4 and, if so, say that it is a leap year(*). However, as a Unix person, you have an alternative. All you have to do is display the numbers of the days for December of that year. If December ends on day 366, you have found a leap year. Otherwise, it's not a leap year. Try these commands and see for yourself:

cal -j 12 2000
cal -j 12 2008
cal -j 12 1776

By the way, the -j stands for Julian, the name of our modern calendar that has 365 or 366 days per year. For more information, see the box.

* Footnote

The only exception is that centenary years are leap years only if they are divisible by 400. For example, 2000 was a leap year, while 900 was not.

The Julian and Gregorian Calendars

The idea that we should have 365 days during regular years and 366 days during leap years is derived from the Julian Calendar, which was introduced by Julius Caesar in 46 B.C.(*)

But how long, exactly, is an average year? If three out of four years have 365 days and the fourth year has 366 days, a year would be an average of 365.25 days. However, nature is not always cooperative, and this value is too long by a matter of 11 minutes, 10 seconds. By the sixteenth century, this small error had accumulated into about 10 days, which meant that the calendar everyone was using did not match the motions of the sun and stars.

To solve the problem, Pope Gregory XIII decreed in 1582 that the world should modify its calendar so that not all centenary years (1600, 1700, 1800, and so on) should be leap years. Only the centenary years that are divisible by 400 (such as 1600 or 2000) would be leap years. This scheme is called the Gregorian or New Style Calendar. To calibrate the current calendar, Gregory further decreed that 10 days should vanish mysteriously.

If we want to be especially precise, we can say that the modern (Gregorian) calendar is based on a cycle of 400 years, with an average year being 365.2425 days. (An interesting fact is that, under this calendar, one nanocentury is approximately π seconds.)

The Pope delivered his decree in 1582. By 1587, the Catholic European countries had implemented the change. The Protestant countries, however, did not follow for some years, the last holdout being Great Britain, which did not make the change until 1752.

As Great Britain changed, so did the American colonies but, by this time, the error had increased to 11 days. Thus, if you enter the command:

cal 9 1752

you will see that between September 2 and September 14, 1752, there is a gap of 11 days. This gap is necessary for the sun and stars to work properly (at least in Great Britain and America).

* Footnote

Actually, the calendar was developed by a graduate student, but Caesar put his name on the paper.

Jump to top of page

The Unix Reminder Service: calendar

The cal program we just discussed displays a calendar. Unix does have a command named calendar, but it is completely different. The calendar program offers a reminder service based on a file of important days and messages that you create yourself.

All you need to do is make a file named calendar. The calendar program will look for this file in the current directory. (We will discuss the current directory in Chapter 24.) Within this file, you put lines of text in which each line contains a date, followed by a tab character, followed by a reminder note. For example:

October 21<Tab>Tammy's birthday
November 20<Tab>Alex's birthday
December 3<Tab>Linda's birthday
December 21<Tab>Harley's birthday

Since tabs are invisible, you won't see them when you look at the file:

October 21     Tammy's birthday
November 20    Alex's birthday
December 3     Linda's birthday
December 21    Harley's birthday (*)

* Footnote

Gifts are accepted until the end of January.

Once your calendar file is set up, you can enter the calendar command whenever you want:


When you do, the program will check your calendar file and display all the lines that have today's or tomorrow's date. If "today" is a Friday, calendar will display three days worth of reminders.

The intention is that, from time to time, you should add lines to your calendar file, until your life is completely organized. Of course, before you can create such a file, you must know how to use a text editor program. The most important text editors are vi (Chapter 22) and Emacs.

If you want to run the calendar command automatically each time you log in, put it in your .profile file (Bash or Korn Shell), or your .login file (C-Shell). See the individual shell chapters for details.

Some Unix systems come with a built-in calendar file that contains a lot of interesting entries. So, even if you don't want to create your own file, you may want to run the calendar command from time to time, just to see what happens. Try it right now.

For more information about calendar and the format of the information file, take a look at the entry for calendar in the online Unix manual (see Chapter 9). The command to use is:

man calendar

Jump to top of page

Information About Your System:
uptime, hostname, uname

There are several commands you can use to display information about your system. To start, the uptime command displays information about how long your system has been up (that is, running continuously):


Here is some typical output:

11:10AM up 103 days, 6:13, 3 users, load averages: 1.90, 1.49, 1.38

In this case, the system has been up for 103 days, 6 hours and 13 minutes, and there are 3 userids currently logged in. The last three numbers show the number of programs that have been waiting to execute, averaged over the last 1, 5 and 15 minutes respectively. These numbers give you an idea of the load on the system. The higher the load, the more the system is doing.

To find out the name of your computer, use the hostname command. This can come in handy if you are in the habit of logging in to more than one computer. If you forget what system you are using, just enter:


The uname command shows you the name of your operating system. For example, you might enter:


and see:


To find out more details about your operating system, use -a (all information):

uname -a

Here is some sample output:

Linux 2.6.24-3.358 #1
Mon Nov 9 09:04:50 EDT 2008 i686 i686 i386 GNU/Linux

The most important information here is that we are using the Linux kernel, version 2.6.5-1.358.

Jump to top of page

Information About You:
whoami, quota

The whoami command displays the userid you used to log in. This command is handy if you have a variety of userids and you forget which one you are currently using. Similarly, if you come upon a computer that someone has left logged in, whoami will show you the current userid. Just enter:


The whoami command is also useful if you are suddenly struck by amnesia and forget your name. Looking at your userid may give you a clue.

If your system doesn't have a whoami command, try entering the following as three separate words:

who am i

The last command to show you information about yourself is quota. On shared systems, the system manager will sometimes impose a limit as to how much disk storage space each user is allowed to use. To check your limit, enter:


Note: Unix measures disk space in KB or kilobytes; 1 KB = 1024 bytes (characters).

Jump to top of page

Information About Other Users:
users, who, w

In the olden days, Unix computers were shared and, most of the time, a system would have multiple users logged in simultaneously. Indeed, very large Unix computers could support tens — or even several hundred — users at the same time. To find out who was currently logged in, there were several commands you could use. These commands are still useful today if you are sharing a system with other users.

The simplest command is users. Just enter:


and you will see a list of all the userids that are currently logged in, for example:

alex casey harley root tammy

You will remember from Chapter 4 that, within a Unix system, only userids have a real identity. Real people — that is, users — are represented by their userids. Thus, you won't see the names of real people, only userids.

The users command is useful if you use a shared system. I can tell you from my experience that being aware that other people are using the same system as you is a pleasant feeling, especially when you are working by yourself, late at night, in an office or terminal room. Just knowing that there is someone else out there makes you feel connected. Unfortunately, many people have no idea what this feels like because their only experience is that everyone has his or her own computer. They don't know what it feels like to share a system.

You might ask, is there any point in using the users command if you use Unix on your own computer? Most of the time the answer is no: it's not all that exciting to see that you are the only person logged in. However, when you use multiple terminal windows or virtual consoles (see Chapter 6), you are logged in to each one separately and, if you run the users command, your userid will show up more than once. Similarly, if you log into virtual consoles using several different userids, they will all show up. So, at the very least, if you get lonely, you can pretend there are other people on the system.

The next command is the who command that we discussed in Chapter 4. This command shows more information than does users. For each userid, who will show you the name of the terminal, the time that the userid logged in and, if appropriate, the remote computer from which the userid has connected to your system.

Here is the output from a who command that was run on a system to which people connect remotely:

tammy   tty1    Nov 10 21:25
root    tty2    Nov 11 15:12
casey   pts/0   Nov 11 10:07 (luna)
harley  pts/1   Nov 11 10:52 (
alex    pts/2   Nov 11 14:39 (

In this example, userid tammy is logged in using terminal tty1, and root (the superuser) is logged in on terminal tty2. These are virtual consoles on the main computer. As I mentioned in Chapter 7, the name tty is often used as an abbreviation for "terminal". In this example, it happens that a user named Tammy is the system administrator, and she is currently logged in twice: once using her personal userid and once as superuser.

To continue, we see that another userid, casey is logged in from a computer named luna, which is on the local network. Finally, two other userids, harley and alex, are logged in remotely via the Internet.

If you want to find out even more information about the userids on your system, you can use the w command. The name stands for "Who is doing what?" Here is some sample output:

8:44pm up 9 days, 7:02, 3 users, load average: 0.11, 0.02, 0.00
tammy  console -    Wed9am 2days  2:38  0.00s  -bash
harley pts/1   -    12:21   0:00  0.01s 0.01s  w
alex   ttyp0   luna 13:11  20:18 43.26s 1.52s  vi birdlist

The first part of the output shows the system statistics, which we saw earlier in the chapter in our discussion of the uptime command. In this case, the system has been up for 9 days, 7 hours and 2 minutes, and there are 3 userids currently logged in. The last three numbers show the number of programs that have been waiting to execute, averaged over the last 1, 5 and 15 minutes respectively. These numbers give you an idea of the load on the system. The higher the load, the more the system is doing.

Following the first line, we see eight columns of information, as follows.

USER: The userids that are currently logged into the system. In this case, they are tammy, harley and alex.

TTY: The names of the terminals used by the various userids.

FROM: The name of the remote computer from which the userid has logged in. In our example, tammy and harley logged in directly on the host computer. alex, however, has logged in from another computer named luna.

LOGIN@: The time that the userid logged in.

IDLE: How long it has been since the user last pressed a key. This is called IDLE TIME. In this case, tammy has been idle for about 2 days, and alex has been idle for 20 minutes and 18 seconds. If you are waiting for a time-consuming job to finish and you are not typing while you are waiting, the w command will show you being idle.

JCPU: The processor time used by all processes since login. The "J" stands for "jobs".

PCPU: The processor time used by the current process. The "P" stands for "process".

Processor time is expressed either in seconds (for example, 20s) or in minutes and seconds (for example, 2:16). These numbers were more valuable in the days when computers were expensive and processor time was considered a valuable commodity.

WHAT: The command that is currently running. In our example, tammy is running the Bash shell; alex is using the vi editor (see Chapter 22) to edit a file named birdlist; and harley is running the w command.

If we put all this information together, we can infer that tammy has been logged in at the console, with a shell prompt, without doing anything for about 2 days. Perhaps this is the system administrator, who likes to stay logged in at all times. We also see that alex is editing a file but has not done anything for over 20 minutes. Perhaps he or she has been taking a break.

Notice that, whenever you run the w command, you will see yourself running the w command. In our example, this is the case with harley.

By default, the w command shows you information about all the userids logged into the system. If you want information about just one userid, you can enter the name along with the command. For example, let's say you have just logged in and you enter the users command to see who else is logged in. You see:

alex casey harley tammy weedly

If you want to see what weedly is doing, enter:

w weedly

What's in a Name?


In the days of the large mainframe computers, the "brain" of the computer — what we would now call the processor — was big enough to require a large box, which was called the central processing unit or CPU.

Today, even though most people don't use mainframe computers, we still use the term CPU as a synonym for "processor".

Jump to top of page

Locking Your Terminal Temporarily: lock

As I mentioned in Chapter 4, it is a bad idea to walk away from your computer while you are logged in. Someone can come along and, by entering commands under the auspices of your userid, cause a lot of trouble. For example, a mischievous person might delete all your files, send rude email to the system manager in your name, download pornography, and so on.

However, if you do need to step away from your terminal for just a moment, it is irritating to have to log out and in again. This is especially true if you are using one or more terminal windows to log in to remote hosts or you have a whole working environment set up just the way you like it.

Instead, you can use the lock command. This tells Unix that you want to lock your terminal temporarily. The terminal will remain locked until you enter a special password. To use this command, just enter:


Unix will display:


Enter the password that you want to use to unlock the terminal. This password can be anything you want; it has nothing to do with your login password. If fact, it is better to not use your login password. Unix will not echo the password as you type, just in case someone else is looking at your screen. After you enter the password, Unix will display:


This is asking you to retype the password, to ensure that you did not make a mistake.

As soon as you have entered and re-entered the special password, Unix will freeze your terminal. Nothing will happen, no matter what anyone types on the terminal, until you enter the password. (Don't forget to press the <Return> key.) As soon as you enter the password, Unix will reactivate your terminal and you can go back to work.

— hint —

If you are logged into one or more remote hosts, you should lock each session separately.

If you are working in a place where you must share computers and there are people waiting, it is considered bad form to lock your terminal and leave for more than a very short time, say, to eat dinner. Since Unix was developed in an environment based on sharing, the lock command has a built-in limitation: the terminal will unlock automatically after a specific amount of time.

By default, lock will freeze a terminal for 15 minutes. However, if you want to override this default, some versions of lock will let you specify an alternate time limit when you enter the command. After the name of the command, leave a space, and then type - (a hyphen), followed by a number. For example, to lock your terminal for 5 minutes, enter:

lock -5

You might ask, what happens if someone locks a terminal and then leaves for good? Eventually, the command will time out and unlock. If the terminal needs to be reactivated before the lock times out, the system manager can enter the root (superuser) password. lock will always accept the root password, sort of like a master key.

Remember though, if you lock your terminal and don't come back, someone will come along eventually and find your terminal reactivated and logged in under your userid. Whatever trouble they cause under your userid will be your responsibility.

Jump to top of page

Asking Unix to Remind You
When to Leave: leave

As you know, working on a computer can be engrossing, and it is easy to lose track of the time. To help you fulfill your worldly obligations, just enter the command:


As the name implies, you can use leave to remind you when it is time to leave. You can also use it to remind you when it is time to take a break. For example, if you like to get up and stretch every so often, you can ask for a reminder in, say, 20 minutes.

When you enter the command, leave will ask you for a time:

When do you have to leave?

Enter the time that you want to leave in the form hhmm (hours followed by minutes). For example, if you want to leave at 10:33, enter 1033.

You can enter times using either a 12-hour or 24-hour system. For instance, 1344 means 1:44 PM. If you enter a number of hours that is 12 or fewer, leave assumes that it is within the next 12 hours. For instance, if it is 8:00 PM and you enter 855, leave will interpret it to mean 8:55 PM, not 8:55 AM.

An alternate way to enter the leave command is to enter the time right on the command line. After the name of the command, leave a space and type the time. For example, to leave at 10:30, enter:

leave 1030

If you need to leave after a certain time interval, type a + (plus sign) followed by the number of minutes. For example, if you need to leave in 15 minutes, use:

leave +15

Be sure not to leave a space after the + character.

— hint —

When you log out, Unix discards a pending leave command. Thus, if you use leave, but then log out and in again, you will have to run the program again.

Once you have entered the leave command, Unix checks periodically to see how much time is left. When it is five minutes before the time you specified, Unix will display:

You have to leave in 5 minutes.

When there is one minute left, you will see:

Just one more minute!

When the time is up, Unix displays:

Time to leave!

From that point on, Unix will keep nagging you with reminders, once a minute, until you log off:

You're going to be late!

Finally, after ten such reminders, you will see:

You're going to be late! That was the last time I'll tell you. Bye.

Perhaps this program should have been named mother.

— hint —

You can run the leave command automatically when you log in, by putting the command in your initialization file (.profile for Bash or the Korn Shell and .login for the C-Shell).

This means that, each time you log in, you will be asked for the time (or length of time) you want to work. This way, you don't have to keep an eye on the clock: Unix will do it for you.

If you use leave in this way and you don't want it to run for the current session, just press <Return> at the first prompt and the program will abort.

— hint —

The leave program is handy when you need to do something in a short time, and you need a reminder. For example, let's say that, as you are working, you like to get up and stretch every 15-20 minutes. However, you get too immersed in your work to remember to take a break. Simply enter the command:

leave +20

In 15 minutes (5 minutes early), leave will get your attention with a warning. Over the next 5 minutes, leave will display several more warnings.

After your break, just enter the command again, and you'll get a reminder in another 15 minutes(*).

* Footnote

The distinguished doctor Janet G. Travell (1901-1997), who held the office of Personal Physician to U.S. Presidents John Kennedy and Lyndon Johnson, was an expert on the pain and dysfunction of skeletal muscles, as well as on the treatment of chronic pain.

While working on her classic book, Myofascial Pain and Dysfunction: The Trigger Point Manual, Dr Travell found that, in order to maintain her comfort and well-being, she needed to take a short break every 15-20 minutes. During this break, she would stand up, move around, and stretch.

You will find that, while you are working, taking such a break will make you feel a lot more comfortable, especially if you suffer from headaches, backaches or eyestrain. Just use the command leave +20 to remind you when to move around and stretch. Once you start this practice, you will feel an improvement in your comfort level within hours. (Another example of how using Unix is good for your well-being.)

Jump to top of page

A Built-In Calculator: bc

One of the most useful (and least appreciated) Unix programs is bc, a full-fledged, programmable scientific calculator. Many people do not bother learning how to use bc. "I spit on bc," they sneer. "Nobody uses it." Don't be misled. Once you learn how to use bc, you will find it invaluable for quick calculations.

If you use a desktop environment (Chapter 5), you will most likely find some type of GUI-based calculator program to use. These programs look nice — they actually draw a picture of a calculator on your screen — but, for minute-to-minute work or for extensive calculation, bc is much better. Moreover, bc is text- based, which means you can use it from the command line at any terminal.

To explain bc, I will start with a short technical summary. If you don't understand all the mathematical and computer terms, don't worry. In the next few sections, I will explain how to use bc for basic calculations (which is easy) along with a few examples.

The technical summary: bc is a fully programmable mathematical interpreter, which offers extended precision. Each number is stored with as many digits as necessary, and you can specify a scale of up to 100 digits to the right of the decimal point. Numeric values can be manipulated in any base from 2 to 16, and it is easy to convert from one base to another.

You can use bc either by entering calculations from the keyboard, which are interpreted immediately, or by running programs stored in files. The programming syntax of bc is similar to the C programming language. You can define functions and use recursion. There are arrays, local variables, and global variables. You can write your own functions and store them in a file. You can then have bc load and interpret them automatically.

bc comes with a library that contains the following functions: sin, cos, arctan, ln, exponential and Bessel function. (Everybody who knows what a Bessel function is, raise your hand...)

For more information, you can display the online manual description of the bc by using the command:

man bc

(We will discuss the online Unix manual in Chapter 9.)

Jump to top of page

Using bc for Calculations

Most of the time, you will use bc for routine calculations, which is simple. To start the program, enter:


If you want to use the built-in library of mathematical functions (see below), start the program using the -l (library) option:

bc -l

Once you start bc there is no specific prompt; just enter one calculation after another. Each time you press <Return>, bc evaluates what you have typed and displays the answer. For example, if you enter:

122152 + 70867 + 122190

bc will display:


You can now enter a new calculation. If you want to enter more than one calculation on the same line, separate them with semicolons(*). bc will display each result on a separate line. For example, if you enter:

10+10; 20+20

you will see:


* Footnote

As you will see in Chapter 10, when you are working at the shell prompt, you can type more than one Unix command on the same line, by separating the commands with semicolons.

When you are finished working with bc, stop the program by telling it there is no more data. To do this, press ^D, the eof key (see Chapter 7). Alternatively, you can enter the command quit.

Figure 8-1 shows the basic operations available with bc. Addition, subtraction, multiplication, division and square root are straightforward and work as you would expect. Modulo calculates the remainder after a division. For example, 53%10 is 3. Exponentiation refers to taking a number to a power. For example, 3^2 means "3 to the power of 2", which is 9. The power must be a whole number but can be negative. If you use a negative power, enclose it in parentheses, for example, 3^(-1).

Figure 8-1:   bc: Basic operations

Operator Meaning
sqrt(x)square root

bc follows the general rules of algebra: multiplication, division and modulo take precedence over addition and subtraction; exponentiation has precedence over everything. To change the order of evaluation, use parentheses. So, 1+2*3 is 7, where (1+2)*3 is 9.

Aside from the basic operations, bc has a number of useful functions in a special library. These functions are shown in Figure 8-2.

Figure 8-2:   bc: Mathematical functions

Function Meaning
s(x)Sine of x; x is in radians
c(x)Cosine of x; x is in radians
a(x)Arctangent of x; x is in radians
ln(x)Natural logarithm of x
j(n,x)Bessel function of integer order n of x

If you want to use the functions in this library, you need to start bc using the command:

bc -l

When you use this command, bc automatically sets the scale factor to 20 (see below).

As I mentioned earlier, bc can compute to arbitrary precision. That is, it will use as many digits as necessary to perform a calculation. For instance, you can ask it to add two 100-digit numbers. (I tested this.)

However, by default, bc will assume you are working with whole numbers. That is, bc will not keep any digits to the right of the decimal point. If you want to use fractional values, you need to set a scale factor to tell bc how many digits you want to keep to the right of the decimal point. To do this, set the value of scale to the scale factor you want.

For example, to ask for three digits to the right of the decimal point, enter:


From now on, all work will be done to three decimal places. Any extra digits will be truncated.

If at any point you want to check what the scale factor is, simply enter:


and bc will display the current value.

When you start bc, the value of scale is set automatically to 0. One of the most common mistakes is to start calculations without setting a scale factor. For instance, let's say that you have just started bc, and you enter:


bc displays:


You now enter:


bc displays:


Finally, you figure out what the problem is. Your results are being truncated, so you need to set an appropriate scale factor:


Now bc will display what you want to see. (Try it.)

Remember, when you use the mathematical library, bc automatically starts with a scale factor of 20. For this reason, many people always start bc by using bc -l, even if they do not want to use the mathematical library. (I do this all the time.)

Jump to top of page

Using Variables With bc

bc is a lot more than a calculator. It is actually a full-featured mathematical programming language. Like all programming languages, bc allows you to set and use variables.

A variable is a quantity with a name and a value. Within bc, variable names consist of a single lowercase letter; that is, there are 26 variables, from a to z. (Make sure that you do not use uppercase letters; these are used when working with bases — see below.)

To set the value of a variable, use an = (equal sign) character. For example, to set the value of the variable x to 100, enter:


To display the value of a variable, just enter its name. For example:


bc will display the current value of that variable. By default, all variables are assumed to be zero unless you set them otherwise.

You will find that using variables is straightforward and adds a lot of power to your work with bc. Here is an example that illustrates the basic principles.

The Maharaja of Gaipajama has been impressed with your facility in Unix. As a token of his esteem, he offers you twice your weight in rubies, worth $1,000 a pound, and one third of your weight in diamonds, worth $2,000 a pound. (The Maharaja of Gaipajama buys his gems wholesale.)

You weigh 160 pounds. How much is the Maharaja's gift worth? To solve this problem, start bc and enter:


The answer is displayed:


Thus, your gift is worth $426,000.

But wait: once the Maharaja realizes how much his promise will cost him, he says, "Did I say I would give you gems based on your weight in pounds? I should have said kilograms."

Since 1 kilogram is 2.2 pounds, you quickly convert the value of the w variable to kilograms:


Now you re-enter the calculations for the value of rubies and diamonds:


The new answer is displayed:


Thus, by adhering to the metric system, the Maharaja(*) has saved $234,000. At the same time, he has allowed you to demonstrate how to set a new value for a variable, based on its old value, in this case, w=w/2.2.

* Footnote

For more information about the Maharaja of Gaipajama, see Cigars of the Pharaoh by Hergé.

Jump to top of page

Using bc with Different Bases

As you would assume, bc normally uses base 10 arithmetic. (If you don't know what a base is, you can skip this section with impunity.) However, there will be times when you may want to calculate using another base. For example, in computer science, it is sometimes necessary to use base 16 (hexadecimal), base 8 (octal) or base 2 (binary).

bc allows you to specify different bases for input and for output. To do so, there are two special variables that you can set: ibase is the base that will be used for input; obase is the base that will be used for output.

For example, if you want to display answers in base 16, enter:


If you want to enter numbers in base 8, use:


In the last section, I said that, by default, variables have a value of zero until you set them. ibase and obase are exceptions: they are automatically initialized to 10 so you can work in base 10. If you want to work in another base, you can set either of the variables to any value from 2 to 16.

You should appreciate that the values of ibase and obase do not affect how bc manipulates numbers internally. Their only effect is to specify how numbers should be translated during input or output.

To work with bases larger than 10, bc represents the values of 10, 11, 12, 13, 14 and 15 as the uppercase letters A, B, C, D, E and F, respectively. Always remember to use uppercase; if you use lowercase, bc will think you are referring to variables, and the result will not be what you intended.

For convenience, you can use these uppercase letters regardless of what input base you have set. For instance, even if you are working in base 10, the expression A+1 will have the value 11.

As with all variables, you can find out the current values of ibase and obase by entering the names by themselves:

ibase; obase

However, you must be careful. Once you set obase, all output will be displayed in that base, and you may have trouble interpreting what you see. For instance, if you enter:


you will see:


This is because all output is to be displayed in base 16 and, in base 16, the value of "16" is expressed as 10.

Similarly, once you change ibase, you must be careful what you type as input. For example, say that you set:


You now want to set obase to base 10, so you enter:


However, you have forgotten that input is now in base 16, and 10 in base 16 is really "16". Thus, you have just set obase to base 16.

To avoid such errors, use the letters A though F, which retain the same value regardless of the ibase value. Thus, if things become confusing, you can always reset the bases by entering:

obase=A; ibase=A

Here are two examples of changing bases. In the first, you want to add two hexadecimal (base 16) numbers, F03E and 3BAC. Enter:

F03E + 3BAC

bc displays the answer:


In the second example, you want to convert the hexadecimal number FFC1 to binary (base 2). Reset the bases:

obase=A; ibase=A

then enter:

obase=2; ibase=16

bc displays the answer:


— hint —

bc is a lot more than a calculator program. It is a sophisticated mathematical programming system with its own built-in programming language. In this chapter, I have explained only the most basic features. However, if you have some time, I suggest that you explore bc and learn more about what it can do for you.

The best way to do so is to read the online manual page for bc. The command to do so is:

man bc

(We will discuss the online Unix manual in Chapter 9.)

Jump to top of page

Reverse Polish Notation

Originally, the bc program was based on a program called dc (desk calculator). dc is among the oldest Unix programs, even older than the C programming language. In fact, the original version of dc was written in 1970 using the programming language B, the ancestor of C. In a moment, we'll talk more about the relation of bc to dc. For now, though, I'd like to teach you a bit about dc — a very interesting tool in its own right — because, like bc, it is a program you can use right away.

Let's start with a technical description: dc is an interactive, arbitrary precision calculator that emulates a stack machine using Reverse Polish notation.

Obviously, dc is not the type of program that will appeal to everyone: if you have no interest in mathematics or computer science, please feel free to skip this discussion. However, if you are technically inclined, dc is important for you to understand for several reasons.

First, as I mentioned, dc uses what is called Reverse Polish notation. Although the idea may mean nothing to you now, it is an important concept you should appreciate if you are studying mathematics, engineering or computer science.

Second, to learn about dc, you need to understand the idea of a stack (which I will explain), a concept that is important to computer scientists and programmers.

Finally, the type of thinking that is required to use dc is the same type of thinking that is required to use Unix. Thus, taking a few moments to learn about dc and — if you are so inclined — teaching yourself how to use it, will bring you that much closer to becoming a Unix person.

We will start our discussion of dc with an explanation of Reverse Polish notation. In the next section, we will move on to the concept of a stack. Once you understand these two fundamental ideas, you will be able to teach yourself how to use dc by using the online documentation.

In 1920, a Polish mathematician named Jan Lukasiewicz (1878-1956) observed that the way in which we write arithmetical expressions can be made more compact by placing the operators before the operands. In doing this, we are able to write complex expressions without using parentheses or brackets. A short example will illustrate the idea.

Say you want to add 34 to 25 and then multiply the sum by 15. Using standard notation, you would write:

(34 + 25) * 15

Because the operators — in this case + (the plus sign) and * (the multiplication sign) — are placed in between the operands, we call this INFIX NOTATION.

Lukasiewicz's system uses PREFIX NOTATION in which we write the operators first followed by the operands. For example:

* + 34 25 15

To evaluate prefix notation, we process the elements, one at a time, from left to right. In this example, we start with the * operator, which tells us to perform a multiplication as soon as we get two numbers. We then encounter the + operator, which tells us to perform an addition as soon as we get two numbers.

Next, we see two numbers in a row, 34 and 25, so we perform the addition operation, which gives us a sum of 59. Remembering the 59, we keep going and encounter the number 15. We can now perform the multiplication, 59*15, to get the final answer 885.

In honor of Lukasiewicz — who was a renowned mathematician, logician and philosopher — prefix notation is often referred to as POLISH NOTATION(*). For computer scientists, Polish notation is important because it is compact, straightforward, and can be evaluated efficiently.

* Footnote

Jan Lukasiewicz was born on December 21, 1878 in the city of Lwow in Galicia, the largest and most northern province of Austria. Galicia was created as a result of the First Partition of Poland in 1772. Although, technically, Lukasiewicz was Austrian, he was an ethnic Pole by birth and the town of Lwow was dominated by Poles. Today, Lwow is known by the name of Lviv and is the largest city in Western Ukraine.

Here is something interesting: As a young man, my grandfather Irving Hahn (1895-1986) lived in Lwow where he apprenticed to be a barber. Moreover, my birthday is December 21.

In 1957, the Australian computer scientist Charles Hamblin wrote two papers in which he proposed using a variation of Polish notation with a stack-based computing system. (We'll talk about stacks in the next section.) The variation he described was to put the operators after the operands, using what is called POSTFIX NOTATION.

To illustrate postfix notation, let's reconsider the expression above. In postfix notation it would look like this:

34 25 + 15 *

To evaluate, we process the elements from left to right. First, we see the two numbers 34 and 25, which we must remember. Next, we see the + operator, which tells us to add the last two available numbers. In this case, we add 34 and 25 to get 59, which we must remember.

Next, we see the number 15, which we also remember. Finally, we see the * operator, which tells us to multiply two numbers. In this case, the numbers are 59 and 15, which we multiply to get the final answer 885.

Postfix notation is particularly suitable for automated computation because expressions can be evaluated in a straightforward manner, from left to right, by remembering numbers and applying operators as they are encountered. With infix notation, the parentheses and other types of precedence — for instance, multiplication must be done before addition — often require that operations be delayed until other operations are completed. This is not the case with postfix notation.

In honor of Lukasiewicz, postfix notation is often referred to as REVERSE POLISH NOTATION or RPN. Over the years, both Polish notation and Reverse Polish notation have been used in a variety of computer systems. For example, Polish (prefix) notation is used in the Lisp programming language and the Tcl scripting language. Reverse Polish (postfix) notation is used in the Forth programming language and the PostScript page description language.

Perhaps the most well-known use of RPN is as the basis for the HP calculators that have been used for years by scientists and engineers. The first such calculator was the HP 9100, which was introduced in 1968.

Since then, RPN calculators have become very popular because, once you understand RPN, it is much faster and easier to use than the traditional infix notation. For example, when you use an RPN calculator, the results of each computation are displayed immediately. This means that you see the partial results as you enter the calculation, making it much easier to catch errors. This is not the case with a calculator that uses traditional infix notation. If you enter an expression that uses the standard rules of precedence, the results cannot be displayed until the entire calculation is finished.

In 1970, a researcher at Bell Labs, Robert Morris, inspired by the HP calculator, used RPN to develop a Unix-based, interactive calculator program, which he called dc (desk calculator). dc was a wonderful tool, but it did require users to learn how to use RPN.

A few years later, Morris and another researcher, Lorinda Cherry, wrote another program called bc, which allowed users to write calculations using the more traditional infix notation. bc worked by converting its input to RPN and then calling upon dc to do the actual work. In other words, bc was a "front-end" to dc. This allowed people to use whichever system they preferred: postfix notation with dc or infix notation with bc.

Years later, as part of the GNU Project (see Chapter 2), bc was completely rewritten as an independent program. Because many types of modern Unix (including Linux and FreeBSD) use the GNU utilities, chances are that, when you use bc today, you are using a standalone program that does not depend upon dc. dc, of course, is still available on its own.

Jump to top of page

The Stack-Based Calculator: dc

Consider the following example of RPN (postfix) notation:

34 25 + 15 *

dc evaluates this expression in the manner I described in the last section, one element at a time, reading from left to right. Each time dc encounters a number, the value of that number must be remembered. Each time dc encounters an operator, the appropriate operation must be performed and the result must be remembered.

The question arises, how does dc keep track of the various quantities that must be remembered? The answer is, by using what we call a stack.

Within computer science, there are a variety of different DATA STRUCTURES used to hold data. Each type of data structure is capable of storing and retrieving data according to its own set of precise rules. The most common types of data structures are lists, linked lists, associative arrays, hash tables, stacks, queues, deques (double-ended queues), as well as a variety of tree-based structures. In this section, we'll concentrate on stacks because that's what dc uses.

A STACK is a data structure in which data elements are stored and retrieved, one at a time, according to a procedure called "last in, first out" or LIFO. Here is how it works.

The stack starts out empty. To store a data element, you PUSH it onto the stack. The data now resides on the TOP of the stack. You can push as many data elements as you like onto the stack, one at a time. Each time you do so, all the elements on the stack are pushed down one level. Thus, at any time, the top of the stack contains the data that was most recently pushed onto the stack. You can retrieve data from the stack only by taking it off the top. When you do, we say that you POP the stack.

In other words, when you pop the stack, you retrieve the last value pushed on the stack. This is why stacks are described as LIFO (last-in, first-out).

For a concrete example of a stack, imagine a spring-loaded column of plates in a cafeteria. The plates are pushed onto the "stack", one at a time. When you want a plate, you must pop the top one off the stack. You have no access to any of the other plates. If, for some reason, you wanted the bottom plate, you would have to pop off all the others, one at a time.

The dc program uses a stack in just this manner to interpret arithmetic expressions that are expressed in RPN. To do so, dc follows a simple procedure: Read the expression from left to right, one element at a time. If a numeric value is encountered, push it onto the stack. If an operator is encountered, pop the appropriate number of elements off the stack, perform the operation, and push the answer onto the stack.

To illustrate this, let's consider the previous example:

34 25 + 15 *

Here is a step-by-step description of what dc does to interpret this expression:

1. Read the value 34 and push it onto the stack.

   The stack contains: 34

2. Read the value 25 and push it onto the stack.

   The stack contains: 25 34

3. Read the + (addition sign). In order to perform addition, two values are needed so...

4. Pop 25 and 34 off the stack and add them. Push the result (59) onto the stack.

   The stack contains: 59

5. Read the value 15 and push it onto the stack.

   The stack contains: 15 59

6. Read the * (multiplication sign). In order to perform multiplication, two values are needed so...

7. Pop 15 and 59 off the stack and multiply them. Push the result (885) onto the stack.

   The stack contains: 885.

If you don't get the hang of RPN right away, don't worry about it. You can always figure it out by practicing with dc. In fact, the best way to really understand RPN is by experimenting.

In our example, I showed you the contents of the stack at each step of the way. With dc, you don't see the stack. However, at any time you can display the top of the stack by using the p (print)command(*). To show how this works, start dc and enter the following two lines. (Don't forget to press <Return> at the end of each line.)

34 25 + 15 *

* Footnote

As I explained in Chapter 7, because the old Unix terminals printed their output on paper, the term "print" is often used as a synonym for "display". Thus, the dc print command displays the value on the top of the stack. This convention is used with many Unix programs.

After you enter the first line, dc performs the calculation. However, you don't see anything. Once you enter the second line (the p command), dc displays the value of the element at the top of the stack, in this case, 885, which is the result of the previous calculation.

If you would like to see the entire stack, use the f command:


Why doesn't dc automatically print the value of the top of the stack each time you enter a new line? The answer is that if dc printed something every time you entered a line, it would clutter up your screen. Instead, dc (like most Unix programs) is as silent as possible. It is up to you to look at the top of the stack as the need arises.

To help you get started with dc, Figure 8-3 contains a summary of the most important dc commands. Aside from this summary, the best way to teach yourself how to use dc is to read the online manual (see Chapter 9) and experiment. The command to look at the dc manual page is:

man dc

Figure 8-3:   dc: The most important commands

Command Meaning
pPrint top of stack
nPop stack and print value
fPrint entire contents of stack
cClear (empty) the stack
dDuplicate top value of stack
rReverse (swap) top two values on stack
+Pop two values, add, push sum
-Pop two values, subtract, push difference
*Pop two values, multiply, push product
/Pop two values, divide, push quotient
%Pop two values, divide, push remainder
~Pop two values, divide, push quotient, push remainder
^Pop two values, second to power of first, push result
vPop one value, take square root, push result
kPop one value, use it to set precision

The number of digits that dc keeps after the decimal point is called the "precision". The default is 0 digits. To change this, push the number of digits you want onto the stack and then enter the k command. This value can be as large as you want. For example, to change the precision to 14 digits, use:

14 k

To display the current precision, use the K (uppercase "K") command. This pushes the current precision on the stack. You can then use the p command to display the actual value:

K p

Finally, to stop dc you can either press ^D to indicate that there is no more data, or you can use the q (quit) command.

— hint —

If you like mathematical or scientific thinking, you should find it fun to play around with dc. As you do, you will find that dc is more than a diversion. In order to learn how to use the program, you will need to master the ideas of Reverse Polish notation and how to use a stack, both of which are difficult concepts. However, once you do master these ideas, you will find that dc is an efficient, well-designed tool that is easy to use.

If you look back at the very end of Chapter 1, you will see that I made two general comments about Unix: Unix is easy to use, but difficult to learn. Start by learning the basics, then learn whatever you want, in whatever order you want.

Can you see that dc fits into the exact same paradigm? It is easy to use, but difficult to learn. And, to start, you learn the basics (by reading this chapter), and then experiment on your own as you see fit.

So, if you take the time to practice with dc, you will not only be learning how to use an interesting tool, you will be training your mind to think in the way Unix minds should think.

Jump to top of page


Review Question #1:

Describe three different ways that are used to stop a program.

Review Question #2:

Which program do you use to display the time? The date?

Review Question #3:

What is the difference between the cal and calendar programs?

Review Question #4:

How do you display the name of the computer you are using? Your operating system? Your userid?

Applying Your Knowledge #1:

Mickey Mouse was born on November 18, 1928. Display the calendar for that month.

What day of the week was Mickey born on?

What number day within the year was it (Jan 1 = 1, Dec 31 = 366)?

Applying Your Knowledge #2:

The element lutetium is a very heavy, rare, silvery white metal. It is considered to be the most expensive metal in the world. The Maharaja of Gaipajama wants you to babysit with his son. The fee will be 1 gram of lutetium for every 2 hours, and he wants you to work for 5 hours.

Assume that gold costs $25.42 (U.S.) per gram, and 1 gram of lutetium is worth 6 grams of gold. Use the bc program to calculate how much your babysitting fee worth in U.S. dollars. The answer must be accurate to 2 decimal places.

(Once you are finished, look up the Maharaja of Gaipajama on the Internet.)

For Further Thought #1:

At the beginning of the chapter, I made the comment, "Traditionally, one of the ways in which people learn Unix is by having a good time while they are learning." This is usually not the case with other operating systems, such as Microsoft Windows. Why?

For Further Thought #2:

The users, who and w programs all display information about the userids that are currently logged into the system. Do we need three different programs?

Jump to top of page