Choosing a Shell for FreeBSD


A lot of different shells are available for FreeBSD, ranging from bare-bones models to some that are loaded with features. Some shells are very strict as to the command syntaxes they accept, and insist that you type everything correctly; other shells allow you to repeat and edit previously used commands and automatically expand command lines with the Tab key. This chapter doesn't cover all shellssuch an undertaking would be nigh impossiblebut rather looks at some of the more popular ones. All shells listed here can be installed from the ports (in /usr/ports/shells) or packages, as described in Chapter 16, "Installing Additional Software."

The Bourne Shell (sh) and POSIX

The earliest shell still in common use is the Bourne shell, commonly known as sh. The original Bourne shell was developed at Bell Labs by Steven Bourne for the AT&T UNIX operating system. FreeBSD, like many other versions of UNIX, has replaced the Bourne shell with an enhanced version called the POSIX shell. This version provides a number of features that the original sh did not, such as rudimentary command-line editing and access to previously executed commands (done in vi edit mode, invoked by typing set -o vi). However, for daily use, sh is a pretty limited shell, and most users will want to switch to a more advanced shell (sh is the default shell assigned by FreeBSD to new users; see Chapter 13 for more on configuring the default shell).

The strength of sh is its flexibility. Accepting your typed commands is hardly its only function; its much more widespread use is as a scripted command interpreter. You can feed a script filea list of commands (known as a batch file in the MS-DOS world)to an sh process, and sh will run the script and execute its contents, automating even the most complex of command-line operations. Shell scripts can entail a lot more than merely lists of commands, too; with flow control, variables, and case-based processing, shell scripting is its own full-fledged programming language, and you'll learn how to harness it in Chapter 10, "Shell Programming." Although few people use sh for their day-to-day user interface, it still forms the basis for a huge number of shell scripts that you can invoke as commands within FreeBSD.

The C Shell (csh)

The C shell is the traditional BSD shell. The researchers at Berkeley saw all the limitations the original UNIX Bourne shell had, so they created their own shell in the process of developing the BSD operating system. The C shell is so named because its own shell programming syntax looks a lot like the C programming language. The C shell has many enhancements over the Bourne shell owing to its interactive nature, including job control and command history. It also supports a logout script file, a program that executes when you exit the shellsomething most Bourne shells (sh and its descendants) don't have.

The C shell also supports a configuration file that gets read every time a new subshell that is not a login shell is started (the original Bourne shell does not support this).

The C shell is the opposite of the Bourne shell in that it is great for interactive use but absolutely terrible for shell programming. Some programmers insist on writing shell programs in the C shell, but because of its more cryptic and demanding syntax, this can be an exercise in frustration and futility; thus, virtually all shell programs in FreeBSD are written in the sh programming language rather than the incompatible csh one.

Although the C shell is great for interactive use and has been very popular, it is somewhat dated, and better choices are available for an interactive shell.

Note

Modern versions of FreeBSD have replaced the original C shell with the tcsh shell. In fact, the C shell (csh) is often just a link to the tcsh shell. You'll learn more about that shell later in this section.


The Korn Shell (ksh or pdksh)

Not to be outdone by Berkeley, AT&T released the Korn shell in 1986. This shell was written by David Korn, and it was AT&T's answer to the C shell. The Korn shell is backward-compatible with the Bourne shell. Like the C shell, it supports job control, command history, command aliases, and a configuration file for subshells. The Korn shell goes even further, though, and includes a number of very useful enhancements for shell programmers, such as extensibility with new commands and syntax compatibility with many other shells and command interpreters. The Korn shell has been very popular and is included with most commercial versions of UNIX these days (not FreeBSD, however).

pdksh is a public domain version of the Korn shell. This is the version you can get for FreeBSD by installing it from the ports collection, as you'll learn in Chapter 16.

Users who are familiar with vi and Emacs will like the Korn shell, but users new to UNIX will probably prefer the bash shell (discussed next).

The Bourne Again Shell (bash)

The Bourne Again shell is a Bourne-compatible shell developed by the Free Software Foundation (FSF). The bash shell is the default shell on virtually all Linux distributions. Like the Korn shell, its Bourne compatibility means it can be used to run sh scripts. In usage it is similar to the Korn shell but adds even more features, including built-in help, intuitive command-line editing and history editing that use the arrow keys (simply press up and down to navigate your previously issued commands, and press left and right to edit your current command in-line), extremely powerful history functions, and more environment variables than you can shake a stick at.

The only real problem with bash is that it is not standard software on most commercial UNIX versions. Because of this, shell programs written specifically for bash can be less portable across different versions of UNIX than programs written for the Bourne shell.

The tcsh Shell

The tcsh shell is an enhanced version of the C shell. (What does the t stand for? It refers to TENEX, an operating system for the DEC PDP-10, whose command-line behavior tcsh originally was intended to mimic.) It adds many new features to the C shell, including filename completion and command-line editing with intuitive editing controls like in bash (it uses the arrow keys). tcsh also has some nice features that even bash doesn't support, including the capability to watch your back by recognizing potentially dangerous commands and then asking whether you're sure you want to continue (for example, "Are you sure you want to delete ALL files?").

tcsh is a great shell for interactive usein many ways, it's even better than bash. Unfortunately, it is still plagued by many of the problems of the C shell when it comes to shell programming. Again, though, almost nobody writes shell scripts in csh style, and so tcsh's shortcomings in the shell programming arena don't have much bearing on its usability as a daily user-interface shell.

The zsh Shell

The Z Shell, or zsh, is a shell designed to cover all the bases. Its basic design is Korn-like, and it incorporates all the popular features of both bash and tcsh (and it attains a large size and memory footprint because of it); but its unique features such as advanced globbing (filename matching), command line editing, and key binding makes it a favorite for hard-core UNIX users, especially those accustomed to the binding-rich Emacs editor.

Which Shell to Choose?

Which shell, then, should you choose? For most of this chapter, it doesn't really matter. Most of the standard FreeBSD commands work the same way under all shells. It is only when you want to start performing advanced tasks, such as changing shell variables, setting aliases, and writing shell programs, that the differences between the shells really become apparent. In day-to-day use, you'll find that some of the features of more advanced shells (such as the ability to recall your previously issued commands using the arrow keys, and to complete command names with the Tab key) are crucial to your ease of use and peace of mind. This means using pdksh, bash, or the shell endorsed by FreeBSDtcsh.

If you don't plan to do any shell programming, tcsh (which in FreeBSD is the same thing as csh) is a fine choice. If you are interested in learning shell programming, choose pdksh or bash because you won't have to unlearn anything when you start programming, and all the commands you learn for operating within the shell can be applied directly to programming within it. For most of this chapter, command-line demonstrations will be shown using bash, which lays the groundwork for the shell programming discussed in Chapter 10.




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net