[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
System User's Guide: Operating System and Devices

Bourne Shell Commands

When you issue a command in the Bourne shell, it first evaluates the command and makes all indicated substitutions. It then runs the command provided that:

If the command name matches neither a built-in command nor the name of a defined function and the command names an executable file that is a compiled (binary) program, the shell (as parent) spawns a new (child) process that immediately runs the program. If the file is marked executable but is not a compiled program, the shell assumes that it is a shell procedure. In this case, the shell spawns another instance of itself (a subshell), to read the file and execute the commands included in it. The shell also runs a parenthesized command in a subshell. To the end user, a compiled program is run in exactly the same way as a shell procedure. The shell normally searches for commands in file system directories, in this order:

  1. /usr/bin
  2. /etc
  3. /usr/sbin
  4. /usr/ucb
  5. $HOME/bin
  6. /usr/bin/X11
  7. /sbin
  8. Current directory

The shell searches each directory, in turn, continuing with the next directory if it fails to find the command.

Note: The order in which the shell searches directories is determined by the PATH variable. You can change the particular sequence of directories searched by resetting the PATH variable.

If you give a specific path name when you run a command (for example, /usr/bin/sort), the shell does not search any directories other than the one you specify. If the command name contains a / (slash), the shell does not use the search path.

You can give a full path name that begins with the root directory (such as /usr/bin/sort). You can also specify a path name relative to the current directory. If you specify, for example:

bin/myfile

the shell looks in the current directory for a directory named bin and in that directory for the file myfile.

Note: The restricted shell does not run commands containing a / (slash).

The shell remembers the location in the search path of each executed command (to avoid unnecessary exec commands later). If it finds the command in a relative directory (one whose name does not begin with /), the shell must redetermine the command's location whenever the current directory changes. The shell forgets all remembered locations each time you change the PATH variable or run the hash -r command.

This section discusses:

Quoting Characters

Many characters have a special meaning to the shell. Sometimes you want to conceal that meaning. Single (') and double (") quotation marks surrounding a string, or a backslash (\) before a single character allow you to conceal the character's meaning.

All characters, except the enclosing single quotation marks, are taken literally, with any special meaning removed. Thus, the command:

stuff='echo $? $*; ls * | wc'

assigns the literal string echo $? $*; ls * | wc to the variable stuff. The shell does not execute the echo, ls, and wc commands or expand the $? and $* variables and the * (asterisk) special character.

Within double quotation marks, the special meaning of the $ (dollar sign), ` (backquote), and " (double quotation) characters remains in effect, while all other characters are taken literally. Thus, within double quotation marks, command and variable substitution takes place. In addition, the quotation marks do not affect the commands within a command substitution that is part of the quoted string, so characters there retain their special meanings.

Consider the following sequence:

ls *
file1 file2 file3
message="This directory contains `ls * ` " 
echo $message
This directory contains file1 file2 file3

This shows that the * (asterisk) special character inside the command substitution was expanded.

To hide the special meaning of the $ (dollar sign), ` (backquote ), and " (double quotation) characters within double quotation marks, precede these characters with a \ (backslash). When you do not use double quotation marks, preceding a character with a backslash is equivalent to placing it within single quotation marks. Hence, a backslash immediately preceding a new-line character (that is, a backslash at the end of the line) hides the new-line character and allows you to continue the command line on the next physical line.

Signal Handling

The shell ignores INTERRUPT and QUIT signals for an invoked command if the command is terminated with an & (ampersand); that is, if it is running in the background. Otherwise, signals have the values inherited by the shell from its parent, with the exception of the SEGMENTATION VIOLATION signal. For more information, refer to the Bourne shell built-in trap command.

Bourne Shell Compound Commands

A compound command is one of the following:

Unless otherwise stated, the value returned by a compound command is that of the last simple command executed.

Reserved Words

The following reserved words are recognized only when they appear without quotes as the first word of a command:

for            do              done
case           esac
if             then             fi
elif           else
while          until
{              }
(              )
for Identifier [in Word . . .] do List done
                          Sets the Identifier parameter to the word or words specified by the Word parameter (one at a time) and runs the commands specified in the List parameter. If you omit in Word . . ., then the for command runs the List parameter for each positional parameter that is set, and processing ends when all positional parameters have been used.
case Word in Pattern [|Pattern] . . . ) List;; [Pattern [|Pattern] . . . ) List;;] . . . esac
                          Runs the commands specified in the List parameter that are associated with the first Pattern parameter that matches the value of the Word parameter. Uses the same character-matching notation in patterns that are used for file name substitution, except that a / (slash), leading . (dot), or a dot immediately following a slash do not need to match explicitly.
if List then List [elif List then List] . . . [else List] fi
                          Runs the commands specified in the List parameter following the if command. If the command returns a zero exit value, the shell runs the List parameter following the first then command. Otherwise, it runs the List parameter following the elif command (if it exists). If this exit value is zero, the shell runs the List parameter following the next then command. If the command returns a non-zero exit value, the shell runs the List parameter following the else command (if it exists). If no else List or then List is performed, the if command returns a zero exit value.
while List do List done
                          Runs the commands specified in the List parameter following the while command. If the exit value of the last command in the while List is zero, the shell runs the List parameter following the do command. It continues looping through the lists until the exit value of the last command in the while List is non-zero. If no commands in the do List are performed, the while command returns a zero exit value.
until List do List done
                          Runs the commands specified in the List parameter following the until command. If the exit value of the last command in the until List is non-zero, runs the List following the do command. Continues looping through the lists until the exit value of the last command in the until List is zero. If no commands in the do List are performed, the until command returns a zero exit value.
( List ) Runs the commands in the List parameter in a subshell.
{ List; } Runs the commands in the List parameter in the current shell process and does not start a subshell.
Name () { List } Defines a function that is referenced by the Name parameter. The body of the function is the list of commands between the braces specified by the List parameter.

Bourne Shell Built-In Commands

Special commands are built in to the Bourne shell and run in the shell process. Unless otherwise indicated, output is written to file descriptor 1 (stdout) and the exit status is 0 (zero) if the command does not contain any syntax errors. Input and output redirection is permitted.

Refer to the List of Bourne Shell Built-in Commands for an alphabetical listing of these commands.

The following special commands are treated somewhat differently from other special built-in commands:

: (colon)     exec            shift
. (dot)       exit            times
break         export          trap
continue      readonly        wait
eval          return

The Bourne shell processes these commands as follows:

Special Command Descriptions

The Bourne shell provides the following special built-in commands:

Built-In Commands
: Returns a zero exit value.
. File Reads and runs commands from the File parameter, and returns. Does not start a subshell. The shell uses the search path specified by the PATH variable to find the directory containing the specified file.
break [ n ] Exits from the enclosing for, while, or until command loops, if any. If you specify the n variable, the break command breaks the number of levels specified by the n variable.
continue [ n ] Resumes the next iteration of the enclosing for, while, or until command loops. If you specify the n variable, the command resumes at the nth enclosing loop.
cd Directory ] Changes the current directory to Directory. If you do not specify Directory, the value of the HOME shell variable is used. The CDPATH shell variable defines the search path for Directory. CDPATH is a colon-separated list of alternative directory names. A null path name specifies the current directory (which is the default path). This null path name appears immediately after the equal sign in the assignment or between the colon delimiters anywhere else in the path list. If Directory begins with a / (slash), the shell does not use the search path. Otherwise, the shell searches each directory in the CDPATH shell variable.
Note: The restricted shell cannot run the cd shell command.
echo String . . . ]
                          Writes character strings to standard output. Refer to the echo command for usage and parameter information. The -n flag is not supported.
eval [ Argument . . . ]
                          Reads arguments as input to the shell and runs the resulting command or commands.
exec [ Argument . . . ]
                          Runs the command specified by the Argument parameter in place of this shell without creating a new process. Input and output arguments can appear and, if no other arguments appear, cause the shell input or output to be modified. This is not recommended for your login shell.
exit [ n ] Causes a shell to exit with the exit value specified by the n parameter. If you omit this parameter, the exit value is that of the last command executed (the Ctrl-D key sequence also causes a shell to exit). The value of the n parameter can be from 0 to 255, inclusive.
export [ Name . . . ]
                          Marks the specified names for automatic export to the environments of subsequently executed commands. If you do not specify the Name parameter, the export command displays a list of all names that are exported in this shell. You cannot export function names.
hash [ -r ][ Command . . . ]
                          Finds and remembers the location in the search path of each Command specified. The -r flag causes the shell to forget all locations. If you do not specify the flag or any commands, the shell displays information about the remembered commands in the following format:
Hits   Cost   Command
Hits indicates the number of times a command has been run by the shell process. Cost is a measure of the work required to locate a command in the search path. Command shows the path names of each specified command. Certain situations require that the stored location of a command be recalculated; for example, the location of a relative path name when the current directory changes. Commands for which that might be done are indicated by an * (asterisk) next to the Hits information. Cost is incremented when the recalculation is done.
pwd Displays the current directory. Refer to the pwd command for a discussion of command options.
read [ Name . . . ] Reads one line from standard input. Assigns the first word in the line to the first Name parameter, the second word to the second Name parameter, and so on, with leftover words assigned to the last Name parameter. This command returns a value of 0 unless it encounters an end-of-file character.
readonly [ Name . . . ]
                          Marks the name specified by the Name parameter as read-only. The value of the name cannot be reset. If you do not specify any Name, the readonly command displays a list of all read-only names.
return [ n ] Causes a function to exit with a return value of n. If you do not specify the n variable, the function returns the status of the last command performed in that function. This command is valid only when run within a shell function.
set [ Flag [ Argument ] . . . ]
                          Sets one or more of the following flags:
-a
                          Marks for export all variables to which an assignment is performed. If the assignment precedes a command name, the export attribute is effective only for that command execution environment, except when the assignment precedes one of the special built-in commands. In this case, the export attribute persists after the built-in command has completed. If the assignment does not precede a command name, or if the assignment is a result of the operation of the getopts or read commands, the export attribute persists until the variable is unset.
-e Exits immediately if all of the following conditions exist for a command:
  • It exits with a return value greater than 0 (zero).
  • It is not part of the compound list of a while, until, or if command.
  • It is not being tested using AND or OR lists.
  • It is not a pipeline preceded by the ! (exclamation point) reserved word.
-f Disables file name substitution.
-h Locates and remembers the commands called within functions as the functions are defined. (Normally these commands are located when the function is performed; see the hash command.)
-k Places all keyword parameters in the environment for a command, not just those preceding the command name.
-n Reads commands but does not run them. The -n flag can be used to check for shell script syntax errors.
-t Exits after reading and executing one command.
-u Treats an unset variable as an error and immediately exits when performing variable substitution. An interactive shell does not exit.
-v Displays shell input lines as they are read.
-x Displays commands and their arguments before they are run.
-- Does not change any of the flags. This is useful in setting the $1 positional parameter to a string beginning with a - (minus sign).

Using a + (plus sign) rather than a - (minus sign) unsets flags. You can also specify these flags on the shell command line. The $- special variable contains the current set of flags.

Any Argument to the set command becomes a positional parameter and is assigned, in order, to $1, $2, and so on. If you do not specify a flag or Argument, the set command displays all the names and values of the current shell variables.

shift [n] Shifts command line arguments to the left; that is, reassigns the value of the positional parameters by discarding the current value of $1 and assigning the value of $2 to $1, of $3 to $2, and so on. If there are more than 9 command line arguments, the 10th is assigned to $9 and any that remain are still unassigned (until after another shift). If there are 9 or fewer arguments, the shift command unsets the highest-numbered positional parameter that has a value.

The $0 positional parameter is never shifted. The shift n command is a shorthand notation specifying n number of consecutive shifts. The default value of the n parameter is 1.

test Expression | [ Expression ]
                          Evaluates conditional expressions. Refer to the test command for a discussion of command flags and parameters. The -h flag is not supported by the built-in test command in bsh.
times Displays the accumulated user and system times for processes run from the shell.
trap [ Command ] [ n ] . . . 
                          Runs the command specified by the Command parameter when the shell receives the signal or signals specified by the n parameter. The trap commands are run in order of signal number. Any attempt to set a trap on a signal that was ignored on entry to the current shell is ineffective.
Note: The shell scans the Command parameter once when the trap is set and again when the trap is taken.

If you do not specify a command, then all traps specified by the n parameter are reset to their current values. If you specify a null string, this signal is ignored by the shell and by the commands it invokes. If the n parameter is 0 (zero), the specified command is run when you exit from the shell. If you do not specify either a command or a signal, the trap command displays a list of commands associated with each signal number.

type [Name . . . ]
                          For each Name specified, indicates how the shell would interpret it as a command name.
ulimit [-HS] [ -c | -d | -f | -m | -s | -t] [limit]
                          Displays or adjusts allocated shell resources. There are two modes for displaying the shell resource settings, which can either be displayed individually or as a group. The default mode is to display resources set to the soft setting, or the lower bound, as a group.

The setting of shell resources depends on the effective user ID of the current shell. The hard level of a resource can be set only if the effective user ID of the current shell is root. You will get an error if you are not root and you are attempting to set the hard level of a resource. By default, the root user sets both the hard and soft limits of a particular resource. The root user should therefore be careful in using the -S, -H, or default flag usage of limit settings. Unless you are a root user, you can only set the soft limit of a resource. Once a limit has been decreased by a non-root user, it cannot be increased, even back to the original system limit.

To set a resource limit, select the appropriate flag and the limit value of the new resource, which should be an integer. You can only set one resource limit at a time. If more than one resource flag is specified, you receive undefined results. By default, ulimit with only a new value on the command line sets the file size of the shell. Use of the -f flag is optional.

You can specify the following ulimit command flags:

-c
                          Sets or displays core segment for shell.
-d Sets or displays data segment for shell.
-f Sets or displays file size for shell.
-H Sets or displays hard resource limit (root user only)
-m Sets or displays memory for shell.
-s Sets or displays stack segment for shell.
-S Sets or displays soft resource limit.
-t Sets or displays CPU time maximum for shell.
umask [nnn] Determines file permissions. This value, along with the permissions of the creating process, determines a file's permissions when the file is created. The default is 022. When no value is entered, umask displays the current value.
unset [Name . . .] Removes the corresponding variable or function for each name specified by the Name parameter. The PATH, PS1, PS2, MAILCHECK, and IFS shell variables cannot be unset.
wait [n] Waits for the child process whose process number is specified by the n parameter to exit and then returns the exit status of that process. If you do not specify the n parameter, the shell waits for all currently active child processes and the return value is 0.

Command Substitution in the Bourne Shell

Command substitution allows you to capture the output of any command as an argument to another command. When you place a command line within `` (backquotes), the shell first runs the command or commands, and then replaces the entire expression, including the backquotes, with the output. This feature is often used to give values to shell variables. For example, the statement:

today=`date`

assigns the string representing the current date to the today variable. The following assignment saves, in the files variable, the number of files in the current directory:

files=`ls | wc -l`

You can perform command substitution on any command that writes to standard output.

To nest command substitutions, precede each of the nested backquotes with a \ (backslash), as in:

logmsg=`echo Your login directory is \`pwd\``

You can also give values to shell variables indirectly by using the read special command. This command takes a line from standard input (usually your keyboard) and assigns consecutive words on that line to any variables named. For example:

read first init last

takes an input line of the form:

J. Q. Public

and has the same effect as if you had typed:

first=J. init=Q. last=Public

The read special command assigns any excess words to the last variable.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]