Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
2.  Logging In and Using Basic SunOS Commands Command Prompt  Previous   Contents   Next 
   
 

Typing Commands

When you see the command prompt, the system is waiting for you to type a command. Try typing the command date at the prompt, as shown in this example (type date and press the Return key):

$ date
Mon Sep 17 10:12:51 PST 2001
$

This command displays the current date and time. When you type the same command, but capitalized, you receive the following message.

$ Date
Date: Command not found.
$

The Solaris operating environment interprets an uppercase D differently than a lowercase d, and the Date command fails. Most commands in the Solaris operating environment are lowercase.

Correcting Typing Mistakes

The commands you type are not sent to the system until you press Return. If you type a command incorrectly, but do not press Return, you can correct your mistake in the following ways.

  • Press the Delete or Back Space key to move back a space 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 methods and see how they work. The Delete/Back Space key varies on some systems. Ctrl-U should work on most systems.

Typing Multiple Commands and Long Commands

You can type 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
Tue Oct 31 15:16:00 MST 2000
spooky

This command entry 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 long command, you can use the backslash character (\) to continue typing on a second line. For example:

$ date; \
logname
Tue Oct 31 15:17:30 MST 2000
spooky

Although the date and logname commands are not long commands, they demonstrate the concept of continuing a set of commands on the next line. Later, when the commands you want to use are longer than the width of your screen, you will see how useful the backslash character can be.


Note - If you use a desktop window, you might not 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 type wrap to the next line automatically, and the system executes all commands when you press Return.


Repeating Previous Commands

The Korn, Bourne Again, C, TC, and Z shells enable your system to keep a history of commands you type and are able to repeat previous commands.


Note - The Bourne shell (sh) does not support the history command.


Repeating Commands in the Bourne Again, C, TC, or Z Shell

If you use the Bourne Again, C, TC, or Z shell, type !! and press Return to repeat the last command you typed.

example%!!
date
Tue Oct 31 15:18:38 MST 2000
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 $HOME
5  logname
6  date
7  history

Note - The Z shell does not display the history command in the history list.


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, type the following command.

example% !-2
date
Tue Oct 31 15:20:41 MST 2000
example%

Note - If you use this command repetition method immediately after the history command in the Z shell, increase the negative number after the ! by one (!-3).


Using the previous example history list, the date command is repeated.

You can also type the ! character, followed by the first few characters of a previous command to repeat that command. For example, if you had previously typed 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 repeats the most recent command that begins with that letter.

Repeating Commands in the Korn Shell

If you use the Korn shell, type the following command to repeat the previous command.

$ fc -s -
date
Tue Oct 31 15:18:38 MST 2000
$

You can also repeat any previously typed command by typing fc -s x, where x is the desired command's corresponding number on the history list. To see the history list, type the fc -l command and press Return. The following example is a sample history list.

$ fc -l
344  pwd
345  clear
346  ls -l
347  cd $HOME
348  logname
349  date
350  history
$

You can also repeat commands from the history list by following the fc -s command with a negative number. For example, to repeat the second from the last command on the history list, type the following command.

$ fc -s -2
date
Tue Oct 31 15:20:41 MST 2000
$

Using the previous example history list, the date command repeats.

You can also use the fc -s command with the first few characters of a previous command. For example, if you had previously typed the date command to display the current date, you could type fc -s da to display the date again. 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 fc -s command, the system repeats the most recent command that begins with that letter.

Adding Command Options

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
Tue Oct 31 22:33:16 GMT 2000
$

Most options are expressed as a single character preceded by a dash (-). Not all commands have options. Some commands have more than one option. If you use more than one option for a command, you can either type the options separately (-a -b) or together (-ab).

Redirecting and Piping Command Output

Unless you indicate otherwise, commands normally display their results on the screen. Some special symbols allow you to redirect the output of a command. For example, you might 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. You can display the contents of sample.file by typing the more command.

$ more sample.file
Tue Oct 31 15:34:45 MST 2000
$

As you can see, the contents of sample.file now contain the output from the date command. See Chapter 3, Working With Files and Directories for information on the more command.

Sometimes you might want to redirect the output of one command as input to another command. A set of commands strung together in this way 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 might 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, type the following:

$ date | lp
request id is jetprint-46 (1 file)
$

This pipeline would print the results of the date command. See "Submitting Print Requests to the Default Printer" for information on using the lp command to print files.

The command redirection examples shown here are simple, but when you learn more advanced commands, you will find many uses for piping and redirection.

 
 
 
  Previous   Contents   Next