Running the Shell


When you log in to the system, a shell program is automatically started for you. This is your login shell. The particular shell program that is run when you log in is determined by your entry in the file /etc/passwd. This file contains information the system needs to know about each user, including name, login ID, and so forth. The last field of this file contains the name of the program to run as your shell. A typical entry might look like

 $ grep username /etc/passwd username:x:3943:100:User Name:/home/username:/bin/bash

To view your own entry in /etc/passwd, just enter the command line shown, with your username. The command grep will search the file for the line with your username. (Chapter 19 describes the use of grep in detail.)

If you are using a graphical interface to UNIX, you may need to open a terminal window (such as xterm, gnome-terminal, or konsole) in order to use the shell. By default, your terminal program will run the same shell as your login shell.

Changing Your Login Shell

In general, you will probably want to keep the login shell that was assigned to you when your account was created. On a multiuser system, it can be a good idea to stick with the shell everyone else is using. Even if you are the only user on the system, the default shell has already been configured for you, which can be helpful.

In some cases, you may want to change your login shell. You may need a specific feature that your default shell lacks-for example, sh and csh lack some very important features of the newer shells, and tcsh is often criticized for not being compatible with Bourne shell scripts. Another reason would be if you are already very familiar with a particular shell and find it easier to continue using that shell.

As far as basic features are concerned, all of the common shells have now become very similar, although they did not start out that way For example, the availability of job control features in sh now removes one of the major differences between sh and csh. If you are trying to choose a shell to learn, you may want to consider bash. Not only is it the default choice on Linux systems, but it successfully combines most of the features of the Korn shell with some of the best features of the C shell, while remaining compatible with the original Bourne-shell syntax.

The command to change your login shell is chsh. In this example, the user rlf is changing her shell to bash:

 $ chsh Changing shell for rlf. Password: New shell [/bin/csh]: /bin/bash Shell changed. $

As you can see, rlf will need to enter her password before she can change her shell. Her current shell is /bin/csh. To change it, she needs to know the full pathname for the new shell.

To find the full pathname for a command or executable, type which followed by the name of the command. For example,

 $ which bash /bin/bash

You can also find the full pathnames for the shells installed on your system in the /etc/shells file.

You will not switch to the new shell until you log out and log back in to the system. On some systems, you may be restricted from changing your login shell. Contact your system administrator if the chsh command is not available.

Logging Out

You log out from the UNIX System by terminating your login shell. There are two ways to do this. You can quit the shell by typing CTRL-D in response to the shell prompt, or you can use the exit command, which terminates your current shell:

 $ exit

If you are using a graphical interface, you can also exit by closing the terminal window.

What the Shell Does

After you log in, much of your interaction with the UNIX System takes the form of a dialog with the shell. The dialog follows this simple sequence repeated over and over:

  1. The shell prompts you when it is ready for input, and waits for you to enter a command.

  2. You enter a command by typing in a command line.

  3. The shell processes your command line to determine what actions to take and carries out the actions required.

  4. After the program is finished, the shell prompts you for input, beginning the cycle again.

The part of this cycle where the real work takes place is the third step-when the shell reads and processes your command line and carries out the instructions it contains. For example, it replaces words in the command line that contain wildcards with the matching filenames. It determines where the input to the command is to come from and where its output goes. After carrying out these and similar operations, the shell runs the program you have indicated in your command, giving it the proper arguments (including options and filenames).

Entering Commands

In general, a command line contains the name of a command, optionally followed by a string of arguments. With a few exceptions (certain keywords like for and while), you end each command line with a newline, which is the UNIX System term for the character produced when you type the ENTER key The shell does not begin to process your command line until you end it with ENTER.

Command-line arguments include options that modify what a command does or how it does it, and information that the command needs, such as the name of a file from which to get data. Options are usually, but not always, indicated with a sign. (As you saw in Chapter 3, the chmod command uses both + and to indicate options, and the tar command described in Chapter 19 does not require a in front of options.)

Your command line may also include arguments and symbols that are really instructions to the shell. For example, when you use the > symbol to direct the output of a command to a file, the shell will process this part of the command line without sending it as an argument to the command. Later in this chapter, you will learn how the shell processes this type of command-line instruction.

In this example of a command line,

 $ ls -l Email > filelist

ls is the name of the command, -l is an option for the command, Email is a filename argument that gets sent to ls, and >filelist is an instruction for the shell.

Argument Expansion

The shell also interprets and replaces certain shortcuts before sending arguments on to commands. This is called expansion. For example, in the modern shells, ~ (tilde) is a shortcut for your home directory So if you enter a command like

 $ mv dance ~

the shell will replace the ~ with your home directory (e.g., /home/raf) before sending the arguments to mv. Similarly, -username is a shortcut for any user’s home directory So if you enter

 $ ln -s ~nate/ProjectFiles

the shell will expand the argument to /home/nate/ProjectFiles. You may have seen this use of ~ in URL’s (web addresses), as in home.webpages.net/~nate.

Tilde expansion is just one example of the shortcuts the shell recognizes. You already saw examples of filename expansion with wildcards in Chapter 3. Later in this chapter you will encounter variable and command expansion (such as $HOME to get the value of the variable HOME, or `ls` to get the value of the ls command).

Grouping Commands

Ordinarily you enter a single command (or a pipeline of two or more joined commands) on each line. If you want, though, you can enter several different commands at once on one line by separating them with a semicolon. For example, the following command line tells the shell to run date first, and then ls -l, just as if you had typed each command on a separate line:

 $ date; ls -l




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

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