built-in commands
This subchapter looks at built-in commands in a UNIX (and Linux) BASH shell.
tools
Most of the commands in the UNIX or Linux shell are actually programs. If you look at the usr/bin directory, you will see the actual programs. Most of these programs were written in the C programming language.
There is a common core of tools/commands that will be available on almost every UNIX or Linux machine, but exactly how many and which commands/tools are available varies widely.
The good news is that if a command or tool is missing from your system, you can go out and get the source code and recompile it for your local computer.
built-in
Many of the shells have special built-in commands. These are not separate programs, but are part of the code for the shell itself.
One example would be the shell command cd that you just saw in the previous quick tour chapter.
There are some built-in commands are only available in selected shells and these can make your scripts shell-dependent.
Some examples of built-in commands include the history command in the C shell, and the export command in the Bourne shell. The cd command is built-in in both bash and csh.
echo is an example of a command that is built into both bash and csh, but also exists externally as a utility.
overriding built-in commands
You can override any built-in commands by giving the full path name to an external command or utility. If bash finds a slash character ( ) anywhere in a command, the shell will not run the built-in command, even if the last component of the specified command matches the name of a builtin command.
As an example, using the command echo will run the version of the command that is built into bash, while specifying /bin/echo or ./echo will ignore the built-in comamnd and instead run the designated utility.
Overriding can be used to run alternative versions of commands or to extend the built-in command to add additional features.
determining builtin or external
You can use the type command to determine if a particular command is a built-in command or an external utility. If the command is an external utility, you will also be told the path to the external command.
$ type echo
echo is a shell builtin
$ type mkdir
mkdir is /bin/mkdir
$
You can use the which command to locate a program in your path.
$ which echo
/bin/echo
$
You can use the whereis command to locate a program in your path.
$ whereis echo
/bin/echo
$
In csh and tcsh you can use the where command to locate a program in your path.
% where echo
/bin/echo
%
problems
If something bad happens to your computer, if the shell is still loaded in memory and running, any of the built-in commands will still work correctly, even if the entire file system (including all hard drives) disappears or becomes unavailable for any reason.
built in command chart
The following chart shows the built-in commands and external utilities for bash and csh for Mac OS X. This wil be similar for Linux and other UNIXes.
External commands marked No** under the External column do exist externally, but are implemented using the built-in command.
Command | External | csh | bash |
! | No | No | Yes |
% | No | Yes | No |
. | No | No | Yes |
: | No | Yes | Yes |
{ | No | No | Yes |
} | No | No | Yes |
alias | No** | Yes | Yes |
alloc | No | Yes | No |
bg | No** | Yes | Yes |
bind | No | No | Yes |
bindkey | No | Yes | No |
break | No | Yes | Yes |
breaksw | No | Yes | No |
builtin | No | No | Yes |
builtins | No | Yes | No |
case | No | Yes | Yes |
cd | No** | Yes | Yes |
chdir | No | Yes | Yes |
command | No** | No | Yes |
complete | No | Yes | No |
continue | No | Yes | Yes |
default | No | Yes | No |
dirs | No | Yes | No |
do | No | No | Yes |
done | No | No | Yes |
echo | Yes | Yes | Yes |
echotc | No | Yes | No |
elif | No | No | Yes |
else | No | Yes | Yes |
end | No | Yes | No |
endif | No | Yes | No |
endsw | No | Yes | No |
esac | No | No | Yes |
eval | No | Yes | Yes |
exec | No | Yes | Yes |
exit | No | Yes | Yes |
export | No | No | Yes |
false | Yes | No | Yes |
fc | No** | No | Yes |
fg | No** | Yes | Yes |
filetest | No | Yes | No |
fi | No | No | Yes |
for | No | No | Yes |
foreach | No | Yes | No |
getopts | No** | No | Yes |
glob | No | Yes | No |
goto | No | Yes | No |
hash | No | No | Yes |
hashstat | No | Yes | No |
history | No | Yes | No |
hup | No | Yes | No |
if | No | Yes | Yes |
jobid | No | No | Yes |
jobs | No** | Yes | Yes |
kill | Yes | Yes | No |
limit | No | Yes | No |
local | No | No | Yes |
log | No | Yes | No |
login | Yes | Yes | No |
logout | No | Yes | No |
ls-F | No | Yes | No |
nice | Yes | Yes | No |
nohup | Yes | Yes | No |
notify | No | Yes | No |
onintr | No | Yes | No |
popd | No | Yes | No |
printenv | Yes | Yes | No |
pushd | No | Yes | No |
pwd | Yes | No | Yes |
read | No** | No | Yes |
readonly | No | No | Yes |
rehash | No | Yes | No |
repeat | No | Yes | No |
return | No | No | Yes |
sched | No | Yes | No |
set | No | Yes | Yes |
setenv | No | Yes | No |
settc | No | Yes | No |
setty | No | Yes | No |
setvar | No | No | Yes |
shift | No | Yes | Yes |
source | No | Yes | No |
stop | No | Yes | No |
suspend | No | Yes | No |
switch | No | Yes | No |
telltc | No | Yes | No |
test | Yes | No | Yes |
then | No | No | Yes |
time | Yes | Yes | No |
times | No | No | Yes |
trap | No | No | Yes |
true | Yes | No | Yes |
type | No | No | Yes |
ulimit | No | No | Yes |
umask | No** | Yes | Yes |
unalias | No** | Yes | Yes |
uncomplete | No | Yes | No |
unhash | No | Yes | No |
unlimit | No | Yes | No |
unset | No | Yes | Yes |
unsetenv | No | Yes | No |
until | No | No | Yes |
wait | No** | Yes | Yes |
where | No | Yes | No |
which | Yes | Yes | No |
while | No | Yes | Yes |
comments, suggestions, corrections, criticisms
free music player coding example
Coding example: I am making heavily documented and explained open source code for a method to play music for free almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
Names and logos of various OSs are trademarks of their respective owners.