4.9. Shells
Prev | Chapter 4. UNIX Basics | Next

## 4.9. Shells

FreeBSD provides a command line interface called a shell. 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 will automatically complete 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 would be 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. Here is a list of common environment variables and their meanings:

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 text pager.
`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(1) because the shell takes all the files that match `*` and echo(1) 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`.

### 4.9.1. Changing Your Shell

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`

### Note:

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).

Prev | Up | Next
4.8. Daemons, Signals, and Killing Processes | Home | 4.10. Text Editors

This, and other documents, can be downloaded from http://ftp.FreeBSD.org/pub/FreeBSD/doc/

For questions about FreeBSD, read the documentation before contacting <questions@FreeBSD.org>.
For questions about this documentation, e-mail <doc@FreeBSD.org>.