Working with the Shell

 < Day Day Up > 

When you log in and are working in a textual (nongraphical) environment, and when you are using a terminal emulator window in a graphical environment, you are using the shell as a command interpreter. That is, the shell displays a prompt, you type a command, and the shell executes the command and displays another prompt.

This section tells you how to identify the shell you are using and explains the keystrokes you can use to correct mistakes on the command line. It covers how to abort a running command and briefly discusses how to edit a command line. Several chapters of this book are dedicated to shells: Chapter 5 introduces shells, Chapter 8 goes into more detail about the Bourne Again Shell with some coverage of the TC Shell, Chapter 9 covers the TC Shell exclusively, and Chapter 11 discusses writing programs (shell scripts) using the Bourne Again Shell.

Which Shell Are You Running?

This book discusses both the Bourne Again Shell (bash) and the TC Shell (tcsh). You are probably running bash, but you may be running tcsh or another shell such as the Z Shell (zsh). You can identify the shell you are running by using the ps utility. Type ps in response to the shell prompt and press RETURN.

 $ ps   PID TTY          TIME CMD  2402 pts/5    00:00:00 bash  7174 pts/5    00:00:00 ps 

This command shows that you are running two utilities or commands: bash and ps. If you are running tcsh, ps will display tcsh instead of bash. If you are running a different shell, ps will display its name.

Correcting Mistakes

This section explains how to correct typographical and other errors you may make while you are logged in on a character-based display. Because the shell does not begin to interpret the command line or other text until you press RETURN, you can correct typing mistakes before you press that key.

You can correct typing mistakes in several ways: You can erase one character at a time, back up a word at a time, or back up to the beginning of the command line in one step. After you press RETURN, it is too late to correct a mistake. You must either wait for the command to run to completion or abort execution of the program (page 27).

Erasing a Character

While entering characters from the keyboard, you can back up and erase a mistake by pressing the erase key once for each character you want to delete. The erase key backs over as many characters as you wish. Usually it will not back up past the beginning of the line.

The default erase key is BACKSPACE. If this key does not work, try DELETE or CONTROL-H. If these keys do not work, give the following command to set the erase and line kill keys (see "Deleting a Line" on page 27) to their defaults:

 $ stty ek 

For information on changing which key erases characters, refer to page 781.

tip: CONTROL-Z suspends a program

Although not a way of correcting a mistake, you may press the suspend key (typically CONTROL-Z) by mistake and wonder what happened (you will see a message containing the word Stopped). You have just stopped the job you were running, using job control (page 271). Give the command fg to continue your job in the foreground, and you should return to where you were before you pressed the suspend key. For more information refer to "bg: Sends a Job to the Background: Sends a Job to the Background" on page 273.


Deleting a Word

You can delete a word you entered by pressing CONTROL-W. A word is any sequence of characters that does not contain a SPACE or TAB. When you press CONTROL-W , the cursor moves left to the beginning of the current word (as you are entering a word) or the previous word (when you have just entered a SPACE or TAB), removing the word it passes over.

Deleting a Line

Any time before you press RETURN, you can delete a line you are entering by pressing the line kill key, also called the kill key. When you press this key, the cursor moves to the left, erasing characters as it goes, back to the beginning of the line. The default line kill key is CONTROL-U. If this key does not work, try CONTROL-X. If neither key works, give the following command to set the erase and line kill keys to their defaults:

 $ stty ek 

For information on changing which key deletes a line, refer to page 781.

Aborting Execution

Sometimes you may want to terminate a running program. For example, a Linux program may be performing a lengthy task such as displaying the contents of a file that is several hundred pages long or copying a file that is not the one you meant to copy.

To terminate a program from a character-based display, press the interrupt key (CONTROL-C or sometimes DELETE or DEL). When you press this key, the Linux operating system sends a terminal interrupt signal both to the program you are running and to the shell. Exactly what effect this signal has depends on the program. Some programs stop execution immediately; others ignore the signal. Some programs take other actions. When it receives a terminal interrupt signal, the shell displays a prompt and waits for another command. For information on changing which key aborts execution, refer to page 781.

If these methods do not terminate the program, try stopping the program with the suspend key (typically CONTROL-Z), giving the jobs command to verify the job number of the program, and using kill to abort the program. The job number is the number within the brackets at the left end of the line that jobs displays ([1]). The kill command sends a signal to the job specified as its argument. You must precede the job number with a percent sign (%1):

 $ bigjob ^Z [1]+  Stopped                 bigjob $ jobs [1]+  Stopped                 bigjob $ kill %1 [1]+  Stopped                 bigjob $ RETURN [1]+  Killed                  bigjob 

By default kill sends a software termination signal ( TERM). When this signal does not work, try using a kill ( KILL) signal:

 $ kill -KILL %1 

A running program cannot ignore a kill signal it is sure to abort the program. The kill command returns a prompt; press RETURN again to see the confirmation message. For more information on job control, refer to "Running a Program in the Background" on page 125. For a list of signals, see Table 11-5 on page 494.

Repeating/Editing Command Lines

To repeat a previous command on the command line, press the UP ARROW key. Each time you press UP ARROW , you see an earlier command line. To reexecute the displayed command line, press RETURN. Press DOWN ARROW to browse through the command lines in the other direction.

The RIGHT and LEFT ARROW keys move the cursor back and forth along the displayed command line. At any point along the command line, you can add characters by typing. Use the erase key to remove characters from the command line.

For more complex command line editing, see page 297 (bash) and page 353 (tcsh).

     < Day Day Up > 


    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net