Jump to content

Bourne Shell Scripting/Appendix A: Command Reference

From Wikibooks, open books for an open world

The Bourne Shell offers a large number of built-in commands that you can use in your shell scripts. The following table gives an overview:

Bourne Shell command reference
Command Description
: A null command that returns a 0 (true) exit value.
. file Execute. The commands in the specified file are read and executed by the shell. Commonly referred to as sourcing a file.
# Ignore all the text until the end of the line. Used to create comments in shell scripts.
#!shell Interpreter hint. Indicates to the OS which interpreter to use to execute a script.
bg [job] ... Run the specified jobs (or the current job if no arguments are given) in the background.
break [n] Break out of a loop. If a number argument is specified, break out of n levels of loops.
case See Bourne Shell Scripting/Control flow
cd [directory] Switch to the specified directory (default $HOME).
continue [n] Skip the remaining commands in a loop and continue the loop at the next interation. If an integer argument is specified, skip n loops.
echo string Write string to the standard output.
eval string ... Concatenate all the arguments with spaces. Then re-parse and execute the command.
exec [command arg ...] Execute command in the current process.
exit [exitstatus] Terminate the shell process. If exitstatus is given it is used as the exit status of the shell; otherwise the exit status of the last completed command is used.
export name ... Mark the named variables or functions for export to child process environments.
fg [job] Move the specified job (or the current job if not specified) to the foreground.
for See Bourne Shell Scripting/Control flow.
hash -rv command ... The shell maintains a hash table which remembers the locations of commands. With no arguments whatsoever, the hash command prints out the contents of this table. Entries which have not been looked at since the last cd command are marked with an asterisk; it is possible for these entries to be invalid.

With arguments, the hash command removes the specified commands from the hash table (unless they are functions) and then locates them. The -r option causes the hash command to delete all the entries in the hash table except for functions.

if See Bourne Shell Scripting/Control flow.
jobs This command lists out all the background processes which are children of the current shell process.
-signal] PID ... Send signal to the jobs listed by ID. If no signal is specified, send SIGTERM.

If the -l option is used, lists all the signal names defined on the system.

newgrp [group] Temporarily move your user to a new group. If no group is listed, move back to your user's default group.
pwd Print the working directory.
read variable [...] Read a line from the input and assign each individual word to a listed variable (in order). Any leftover words are assigned to the last variable.
readonly name ... Make the listed variables read-only.
return [n] Return from a shell function. If an integer argument is specified it will be the exit status of the function.
set [{ -options | +options | -- }] arg ... The set command performs three different functions.

With no arguments, it lists the values of all shell variables.

If options are given, it sets the specified option flags or clears them.

The third use of the set command is to set the values of the shell's positional parameters to the specified args. To change the positional parameters without changing any options, use “--” as the first argument to set. If no args are present, the set command will clear all the positional parameters (equivalent to executing “shift $#”.)

shift [n] Shift the positional parameters n times.
test See Bourne Shell Scripting/Control flow.
trap [action] signal ... Cause the shell to parse and execute action when any of the specified signals are received.
type [name ...] Show whether a command is a UNIX command, a shell built-in command or a shell function.
ulimit Report on or set resource limits.
umask [mask] Set the value of umask (the mask for the default file permissions not assigned to newly created files). If the argument is omitted, the umask value is printed.
unset name ... Drop the definition of the given names in the shell.
wait [job] Wait for the specified job to complete and return the exit status of the last process in the job. If the argument is omitted, wait for all jobs to complete and the return an exit status of zero.
while See Bourne Shell Scripting/Control flow.