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
zsh
and bash
.
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 automatically completes
the rest of the command or filename. Consider two files called
foobar
and foo.bar
.
To delete foo.bar
, type rm
fo[Tab].[Tab]
.
The shell should print out
rm foo[BEEP].bar
.
The [BEEP] is the console bell, which the shell used to
indicate it was unable to complete the filename because there
is more than one match. Both foobar
and
foo.bar
start with fo
.
By typing .
, then pressing
Tab again, the shell is able to fill in the
rest of the filename.
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. Table 4.3 provides a list of common environment variables and their meanings. Note that the names of environment variables are always in uppercase.
Variable | Description |
---|---|
USER | Current logged in user's name. |
PATH | Colon-separated list of directories to search for binaries. |
DISPLAY | Network name of the Xorg display to connect to, if available. |
SHELL | The current shell. |
TERM | The name of the user's type of terminal. Used to determine the capabilities of the terminal. |
TERMCAP | Database entry of the terminal escape codes to perform various terminal functions. |
OSTYPE | Type of operating system. |
MACHTYPE | The system's CPU architecture. |
EDITOR | The user's preferred text editor. |
PAGER | The user's preferred utility for viewing text one page at a time. |
MANPATH | 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
sh(1) and bash
, use
export
to set the current environment
variables. This example sets the default EDITOR
to /usr/local/bin/emacs
for the
tcsh(1) shell:
%
setenv EDITOR /usr/local/bin/emacs
The equivalent command for bash
would be:
%
export EDITOR="/usr/local/bin/emacs"
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
$TERM
setting.
Shells treat special characters, known as meta-characters,
as special representations of data. The most common
meta-character is *
, which represents any
number of characters in a filename. Meta-characters can be used
to perform filename globbing. For example, echo
*
is equivalent to ls
because
the shell takes all the files that match *
and echo
lists them on the command
line.
To prevent the shell from interpreting a special character,
escape it from the shell by starting it with a backslash
(\
). For example, echo
$TERM
prints the terminal setting whereas
echo \$TERM
literally prints the string
$TERM
.
The easiest way to permanently change the default shell is
to use 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 Shell:
line to the full path of the new shell.
Alternately, use chsh -s
which will set
the specified shell without opening an editor. For example,
to change the shell to bash
:
%
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
Chapter 5, 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:
#
echo /usr/local/bin/bash >> /etc/shells
Then, rerun chsh(1).
All FreeBSD documents are available for download at http://ftp.FreeBSD.org/pub/FreeBSD/doc/
Questions that are not answered by the
documentation may be
sent to <freebsd-questions@FreeBSD.org>.
Send questions about this document to <freebsd-doc@FreeBSD.org>.