Classic Shell Scripting - Arnold Robbins [96]
Read file and execute its contents in current shell.
alias
Set up shorthand for command or command line (interactive use).
bg
Put job in background (interactive use).
breaka
Exit from surrounding for, while, or until loop.
cd
Change working directory.
command
Locate built-in and external commands; find a built-in command instead of an identically named function.
continuea
Skip to next iteration of for, while, or until loop.
evala
Process arguments as a command line.
execa
Replace shell with given program or change I/O for shell.
exita
Exit from shell.
exporta
Create environment variables.
false
Do nothing, unsuccessfully.
fc
Work with command history (interactive use).
fg
Put background job in foreground (interactive use).
getopts
Process command-line options.
jobs
List background jobs (interactive use).
kill
Send signals.
newgrp
Start new shell with new group ID (obsolete).
pwd
Print working directory.
read
Read a line from standard input.
readonlya
Make variables read-only (unassignable).
returna
Return from surrounding function.
seta
Set options or positional parameters.
shifta
Shift command-line arguments.
timesa
Print accumulated user and system CPU times for the shell and its children.
trapa
Set up signal-catching routine.
true
Do nothing, successfully.
umask
Set/show file permission mask.
unalias
Remove alias definitions (interactive use).
unseta
Remove definitions of variables or functions.
wait
Wait for background job(s) to finish.
[6] The source command in bash (borrowed from the BSD C shell) is equivalent to the dot command.
The distinction between special and regular built-in commands comes into play when the shell searches for commands to execute. The command-search order is special built-ins first, then shell functions, then regular built-ins, and finally external commands found by searching the directories listed in $PATH. This search order makes it possible to define shell functions that extend or override regular shell built-ins.
This feature is used most often in interactive shells. For example, suppose that you would like the shell's prompt to contain the last component of the current directory's pathname. The easiest way to make this happen is to have the shell change PS1 each time you change directories. You could just write your own function:
# chdir --- private function to update PS1 when changing directories
chdir ( ) {
cd "$@" Actually change directory
x=$(pwd) Get current directory name into variable x
PS1="${x##*/}\$ " Lop off leading components, assign to PS1
}
The problem is that you have to remember to type chdir at the shell instead of cd, and if you accidentally forget and type cd, you'll be in the new directory, but the prompt won't be changed. For this reason, you can write a function named cd, and the shell will find your function first, since cd is a regular built-in:
# cd --- private version to update PS1 when changing directories
# (won't actually work, see text)
cd ( ) {
cd "$@" Actually change directory?!?
x=$(pwd) Get current directory name into variable x
PS1="${x##*/}\$ " Lop off leading components, assign to PS1
}
There is one small fly in the ointment here. How does the shell function access the functionality of the "real" cd command? The cd "$@" shown here just calls the function again, leading to infinite recursion. What's needed is an "escape hatch" that tells the shell to bypass the search for functions and access the real command. This is the job of the command built-in command, whose use is shown in Example 7-4.
Example 7-4. Updating PS1 when changing directories
# cd --- private version to update PS1 when changing directories
cd ( ) {
command cd "$@" Actually change directory
x=$(pwd) Get current directory name into variable x
PS1="${x##*/}\$ " Lop off leading components, assign to PS1
}