< Day Day Up > |
13.3. The Command LineAfter you log on, the bash shell displays its primary prompt, a dollar sign by default. The shell is your command interpreter. When the shell is running interactively, it reads commands from the terminal and breaks the command line into words. A command line consists of one or more words (or tokens), separated by whitespace (blanks and/or tabs), and terminated with a newline, generated by pressing the Enter key. The first word is the command, and subsequent words are the command's arguments. The command may be a UNIX/Linux executable program such as ls or date, a user-defined function, a built-in command such as cd or pwd, or a shell script. The command may contain special characters, called metacharacters, which the shell must interpret while parsing the command line. If a command line is too long, the backslash character, followed by a newline, will allow you to continue typing on the next line. The secondary prompt will appear until the command line is terminated. 13.3.1 The Order of Processing CommandsThe first word on the command line is the command to be executed. The command may be a keyword, an alias, a function, a special built-in command or utility, an executable program, or a shell script. The command is executed according to its type in the following order:
Special built-in commands and functions are defined within the shell, and therefore are executed from within the context of the current shell, making them much faster in execution. Scripts and executable programs such as ls and date are stored on disk, and the shell, in order to execute them, must first locate them within the directory hierarchy by searching the PATH environment variable; the shell then forks a new shell that executes the script. To find out the type of command you are using—that is, a built-in command, an alias, a function, or an executable—use the built-in type command. (See Example 13.18.) Example 13.18.$ type pwd pwd is a shell builtin $ type test test is a shell builtin $ type clear clear is /usr/bin/clear $ type m m is aliased to 'more' $ type bc bc is /usr/bin/bc $ type if if is a shell keyword $ type -path cal /usr/bin/cal $ type which which is aliased to 'type -path' $ type greetings greetings is a function greetings () { echo "Welcome to my world!"; } 13.3.2 Built-In Commands and the help CommandBuilt-in commands are commands that are part of the internal source code for the shell. They are built-in and readily available to the shell, whereas commands such as date, cal, and finger are compiled binary programs that reside on the disk. There is less overhead in executing a built-in because it involves no disk operations. Built-in commands are executed by the shell before executable programs on disk. Bash has added a new online help system so that you can see all the built-ins, or a description for a particular built-in; help itself is a built-in command. See Table 13.28 on page 857 for a complete list of built-in commands. Example 13.19.1 $ help help help: help [pattern ...] Display helpful information about built-in commands. if PATTERN is specified, gives detailed help on all commands matching PATTERN, otherwise a list of the built-ins is printed. 2 $ help pw pwd: pwd Print the current working directory.
13.3.3 Changing the Order of Command-Line ProcessingBash provides three built-in commands that can override the order of command-line processing: command, builtin, and enable. The command built-in eliminates aliases and functions from being looked up in the order of processing. Only built-ins and executables, found in the search path, will be processed. The builtin command looks up only built-ins, ignoring functions and executables found in the path. The enable built-in command turns built-ins on and off. By default, built-ins are enabled. Disabling a built-in allows an executable command to be executed without specifying a full pathname, even if it has the same name as a built-in. (In normal processing, bash searches for built-ins before disk executable commands.) Built-ins become disabled by using the –n switch. For example, a classic confusion for new shell programmers is naming a script test. Because test is a built-in command, the shell will try to execute it rather than the user's script (because a built-in is normally executed before any executable program). By typing: enable –n test, the test built-in is disabled, and the user's script will take precedence. Without options, the enable built-in prints a list of all the built-ins. Each of the following built-ins are described in "Shell Built-In Commands" on page 857. Example 13.20.1 $ enable enable . enable : enable [ enable alias enable bg enable bind enable break enable builtin enable cd enable command enable continue enable declare enable dirs ..... enable read enable readonly enable return enable set enable shift enable shopt ..... enable type enable typeset enable ulimit enable umask enable unalias enable unset enable wait 2 enable -n test 3 function cd { builtin cd; echo $PWD; } EXPLANATION
13.3.4 The Exit StatusAfter a command or program terminates, it returns an exit status to the parent process. The exit status is a number between 0 and 255. By convention, when a program exits, if the status returned is 0, the command was successful in its execution. When the exit status is nonzero, the command failed in some way. If a command is not found by the shell, the exit status returned is 127. If a fatal signal causes the command to terminate, the exit status is 128 plus the value of the signal that caused it to die. The shell status variable, ?, is set to the value of the exit status of the last command that was executed. Success or failure of a program is determined by the programmer who wrote the program. Example 13.21.1 $ grep ellie /etc/passwd ellie:MrHJEFd2YpkJY:501:501::/home/ellie:/bin/bash 2 $ echo $? 0 3 $ grep nicky /etc/passwd 4 $ echo $? 1 5 $ grep ellie /junk grep: /junk: No such file or directory 6 $ echo $? 2 7 $ grip ellie /etc/passwd bash: grip: command not found 8 $ echo $? 127 9 $ find / -name core ^C # User presses Ctrl-C 10 $ echo $? 130 EXPLANATION
13.3.5 Multiple Commands at the Command LineA command line can consist of multiple commands. Each command is separated by a semicolon, and the command line is terminated with a newline. The exit status is that of the last command in the chain of commands. Example 13.22.
$ ls; pwd; date
EXPLANATION The commands are executed from left to right, one after the other, until the newline is reached. 13.3.6 Command GroupingCommands may also be grouped so that all of the output is either piped to another command or redirected to a file. Example 13.23.
$ ( ls; pwd; date ) > outputfile
EXPLANATION The output of each of the commands is sent to the file called outputfile. The spaces inside the parentheses are necessary. 13.3.7 Conditional Execution of CommandsWith conditional execution, two command strings are separated by the special metacharacters, double ampersands (&&) and double vertical bars (||). The command on the right of either of these metacharacters will or will not be executed based on the exit condition of the command on the left. Example 13.24.
$ cc prgm1.c –o prgm1 && prgm1
EXPLANATION If the first command is successful (has a 0 exit status), the command after the && is executed; that is, if the cc program can successfully compile prgm1.c, the resulting executable program, prgm1, will be executed. Example 13.25.
$ cc prog.c >& err || mail bob < err
EXPLANATION If the first command fails (has a nonzero exit status), the command after the || is executed; that is, if the cc program cannot compile prog.c, the errors are sent to a file called err, and user bob will be mailed the err file. 13.3.8 Commands in the BackgroundNormally, when you execute a command, it runs in the foreground, and the prompt does not reappear until the command has completed execution. It is not always convenient to wait for the command to complete. When you place an ampersand (&) at the end of the command line, the shell will return the shell prompt immediately and execute the command in the background concurrently. You do not have to wait for one command to finish before starting another. The output from a background job will be sent to the screen as it processes. Therefore, if you intend to run a command in the background, the output of that command might be redirected to a file or piped to another device, such as a printer, so that its output does not interfere with the more recent command that was typed. The ! variable contains the PID number of the last job put in the background. (See the following section, "Job Control," for more on background processing.) Example 13.26.1 $ man sh | lp& 2 [1] 1557 3 $ kill -9 $! EXPLANATION
|
< Day Day Up > |