A shell provides a command line
interface for interacting with the operating system. A shell
receives commands from the input channel and executes them.
Many shells provide built in functions to help with everyday
tasks such as file management, file globbing, command line
editing, command macros, and environment variables. FreeBSD comes
with several shells, including the Bourne shell (sh(1)) and
the extended C shell (tcsh(1)). Other shells are available
from the FreeBSD Ports Collection, such as
The shell that is used is really a matter of taste. A C
programmer might feel more comfortable with a C-like shell such
as tcsh(1). A Linux user might prefer
bash. Each shell has unique properties that
may or may not work with a user's preferred working environment,
which is why there is a choice of which shell to use.
One common shell feature is filename completion. After a
user types the first few letters of a command or filename and
presses Tab, the shell completes the rest of
the command or filename. Consider two files called
foobar, the user might type
rm foo and press Tab to
complete the filename.
But the shell only shows
rm foo. It was
unable to complete the filename because both
foo. Some shells sound a beep or
show all the choices if more than one name matches. The user
must then type more characters to identify the desired filename.
t and pressing Tab
again is enough to let the shell determine which filename is
desired and fill in the rest.
Another feature of the shell is the use of environment variables. Environment variables are a variable/key pair stored in the shell's environment. This environment can be read by any program invoked by the shell, and thus contains a lot of program configuration. Table3.4, “Common Environment Variables” provides a list of common environment variables and their meanings. Note that the names of environment variables are always in uppercase.
|Current logged in user's name.|
|Colon-separated list of directories to search for binaries.|
|Network name of the Xorg display to connect to, if available.|
|The current shell.|
|The name of the user's type of terminal. Used to determine the capabilities of the terminal.|
|Database entry of the terminal escape codes to perform various terminal functions.|
|Type of operating system.|
|The system's CPU architecture.|
|The user's preferred text editor.|
|The user's preferred utility for viewing text one page at a time.|
|Colon-separated list of directories to search for manual pages.|
How to set an environment variable differs between shells.
In tcsh(1) and csh(1), use
setenv to set environment variables. In
export to set the current environment
variables. This example sets the default
/usr/local/bin/emacs for the
setenv EDITOR /usr/local/bin/emacs
The equivalent command for
To expand an environment variable in order to see its
current setting, type a
$ character in front
of its name on the command line. For example,
echo $TERM displays the current
Shells treat special characters, known as meta-characters,
as special representations of data. The most common
*, which represents any
number of characters in a filename. Meta-characters can be used
to perform filename globbing. For example,
* is equivalent to
the shell takes all the files that match
echo lists them on the command
To prevent the shell from interpreting a special character,
escape it from the shell by starting it with a backslash
\). For example,
$TERM prints the terminal setting whereas
echo \$TERM literally prints the string
The easiest way to permanently change the default shell is
chsh. Running this command will
open the editor that is configured in the
EDITOR environment variable, which by default
is set to vi(1). Change the
line to the full path of the new shell.
chsh -s which will set
the specified shell without opening an editor. For example,
to change the shell to
chsh -s /usr/local/bin/bash
The new shell must be present in
/etc/shells. If the shell was
installed from the FreeBSD Ports Collection as described in
Chapter4, Installing Applications: Packages and Ports, it should be automatically added
to this file. If it is missing, add it using this command,
replacing the path with the path of the shell:
Then, rerun chsh(1).
The UNIX shell is not just a command interpreter, it acts as a powerful tool which allows users to execute commands, redirect their output, redirect their input and chain commands together to improve the final command output. When this functionality is mixed with built in commands, the user is provided with an environment that can maximize efficiency.
Shell redirection is the action of sending the output or the input of a command into another command or into a file. To capture the output of the ls(1) command, for example, into a file, redirect the output:
ls > directory_listing.txt
The directory contents will now be listed in
directory_listing.txt. Some commands can
be used to read input, such as sort(1). To sort this
listing, redirect the input:
sort < directory_listing.txt
The input will be sorted and placed on the screen. To redirect that input into another file, one could redirect the output of sort(1) by mixing the direction:
sort < directory_listing.txt > sorted.txt
In all of the previous examples, the commands are performing redirection using file descriptors. Every UNIX system has file descriptors, which include standard input (stdin), standard output (stdout), and standard error (stderr). Each one has a purpose, where input could be a keyboard or a mouse, something that provides input. Output could be a screen or paper in a printer. And error would be anything that is used for diagnostic or error messages. All three are considered I/O based file descriptors and sometimes considered streams.
Through the use of these descriptors, the shell allows output and input to be passed around through various commands and redirected to or from a file. Another method of redirection is the pipe operator.
The UNIX pipe operator, “|” allows the output of one command to be directly passed or directed to another program. Basically, a pipe allows the standard output of a command to be passed as standard input to another command, for example:
cat directory_listing.txt | sort | less
In that example, the contents of
directory_listing.txt will be sorted and
the output passed to less(1). This allows the user to
scroll through the output at their own pace and prevent it
from scrolling off the screen.