When you see the command prompt, it means that the system is waiting for you to enter a command. Try entering the command date at the prompt, as shown in this example (type date and press the Return key):
$ date Mon Feb 3 10:12:51 PST 1992 $ |
As you can see, this command displays the current date and time. Now try entering the same command, but capitalized:
$ Date Date: Command not found. $ |
As you can see, an uppercase D is not the same as a lowercase d when interpreted by the system. Nearly all commands in the SunOS operating system are lowercase.
Suppose you started to type Date, but then realized your mistake before pressing the Return key. The text you type is not sent to the system until you press Return. Therefore, it is still possible to correct your mistake. You have two choices:
Press the Delete or Backspace key to backspace to the error; or
Type Ctrl-U to erase the entire line and start over. (Hold down the Control key and press "u".)
Try both of these and see how they work. (The Delete/Backspace key varies on some systems. Ctrl-U should work on most systems.)
You can enter more than one command on a single line. Simply place a semicolon (;) between the commands, as shown here with the date command and the logname command:
$ date; logname Mon Feb 3 10:19:25 PST 1992 spanky $ |
As you can see, this displays the current date and time (from the date command) and the login name of the user currently logged in to the system (from the logname command).
If you are typing a very long command, you can use the backslash character (\) to continue typing on a second line. For example:
$ date; \ logname Mon Feb 3 10:23:25 PST 1992 hankw $ |
Even though the date and logname commands are by no means long commands, they are used in this example to demonstrate the concept of continuing a set of commands on the next line in as simple a manner as possible. Later, when the commands you want to use are longer than the width of your screen, you'll see how using the backslash character can be extremely useful.
If you are using the Shell Tool or Command Tool windows in the OpenWindows environment, you won`t need to use the backslash character to continue typing commands on the next line. When you reach the end of a line, the commands you're typing wrap to the next line automatically, and the system executes all commands when you press Return.
The command repeating features described in this section are available only if you are using the C shell.
A quick way to repeat the last command you typed is to type !! and press Return. The system keeps a history of commands you type and is able to repeat previous commands. For example, if the last command you entered was date:
example% !! date Mon Feb 3 10:26:20 PST 1992 example% |
You can also repeat any previously typed command by typing !x, where x is the desired command's corresponding number on the history list. To see the history list, type the history command and press Return. The following is an example of what you might see:
example% history 1 pwd 2 clear 3 ls -l 4 cd /usr/home/worker 5 logname 6 date 7 history |
Another method for repeating characters from the history list is to follow the ! with a negative number. For example, to repeat the second from the last command on the history list, you would type the following:
example% !-2 logname hankw example% |
Using the example history list above, the logname command is repeated.
Still another method is to follow the ! with the first few characters of a previous command. For example, if you had previously entered the clear command to clear your screen, you could type !cl to clear your screen again. With this method for repeating commands, however, you must use enough characters for the desired command to be unique in the history list. If you use only one letter after the !, the system will repeat the most recent command beginning with that letter.
Many commands have options that invoke special features of the command. For example, the date command has the option -u, which expresses the date in Greenwich Mean Time instead of local time:
$ date -u Mon Feb 3 11:06:51 GMT 1993 $ |
Most options are expressed as a single character preceded by a dash (-). Not all commands have options. Some commands have more than one. If you use more than one option for a command, you can either type the options separately (-a -b) or together (-ab).
Unless you indicate otherwise, commands normally display their results on the screen. There are special symbols that allow you to redirect the output of a command. For example, you may want to save the output to a file rather than display it on the screen. The following example illustrates the use of the redirect symbol (>):
$ date > sample.file $ |
In this example, the output from the date command is redirected to a new file called sample.file. Next, the contents of sample.file are displayed with the more command:
$ more sample.file Mon Feb 3 12:56:26 PST 1993 $ |
As you can see, the contents of sample.file now contains the output from the date command. (See Chapter 3, Working with Files and Directoriesfor information on the more command.)
Sometimes you may want to redirect the output of one command as input to another command. A set of commands strung together in this fashion is called a pipeline. The symbol for this type of redirection is a vertical bar (|) called a pipe.
For example, instead of saving the output of a command to a file, you may want to direct it as input to the command for printing (lp) by using the pipe symbol (|). To send the output from the date command directly to the printer, you would enter the following:
$ date | lp $ |
This would print the results of the date command. (See "8.1.1 Submitting Print Requests to the Default Printer"for information on using the lp command to print files.)
The command redirection examples shown here are quite simple, but when you learn more advanced commands, you will find that there are many uses for piping and redirection.
Often, it is convenient to initiate a command from the command prompt and then place that command in the background. When a command is not placed in the background, the next prompt does not appear until the command completes its task. However, some commands can take a long time to finish, and you might prefer to enter other commands in the meantime.
If you know you want to run a command in the background, type an ampersand (&) after the command as shown below. The number that follows is the process id:
$ bigjob & [1] 21414 $ |
The command bigjob will now run in the background, and you can continue to enter other commands. After the job completes, you will see a message similar to the following the next time you enter another command, such as date in this example:
$ date Mon Feb 3 10:23:25 PST 1992 [1] + Done bigjob $ |
If you are likely to log off before a background job completes, use the nohup (no hangup) command to allow the job to complete, as shown in this example. Otherwise, the background job will be terminated when you log off:
$ nohup bigjob & [1] 21414 $ |