You're reading from Linux Command Line and Shell Scripting Bible - Third Edition
What Is the dash Shell?
The Debian dash shell has had an interesting past. It's a direct descendant of the ash shell, a simple copy of the original Bourne shell available on Unix systems (see Chapter 1). Kenneth Almquist created a small-scale version of the Bourne shell for Unix systems and called it the Almquist shell, which was then shortened to ash. This original version of the ash shell was extremely small and fast but without many advanced features, such as command line editing or history features, making it difficult to use as an interactive shell.
The NetBSD Unix operating system adopted the ash shell and still uses it today as the default shell. The NetBSD developers customized the ash shell by adding several new features, making it closer to the Bourne shell. The new features include command line editing using both emacs and vi editor commands, as well as a history command to recall previously entered commands. This version of the ash shell is also used by the FreeBSD operating...
The dash Shell Features
Although both the bash shell and the dash shell are modeled after the Bourne shell, they have some differences. This section walks you through the features found in the Debian dash shell to acquaint you with how the dash shell works before we dive into the shell scripting features.
The dash command line parameters
The dash shell uses command line parameters to control its behavior. Table 23.1 lists the command line parameters and describes what each one does.
Table 23.1 The dash Command Line Parameters
Parameter | Description |
-a |
Exports all variables assigned to the shell |
-c |
Reads commands from a specified command string |
-e |
If not interactive, exits immediately if any untested command fails |
-f |
Displays pathname wildcard characters |
-n |
If not interactive, reads commands but doesn't execute them |
-u |
Writes an error message to STDERR when attempting to expand a variable that is not set |
-v |
Writes input to STDERR as it is read |
-x |
Writes each command... |
Scripting in dash
Unfortunately, the dash shell doesn't recognize all the scripting features of the bash shell. Shell scripts written for the bash environment often fail when run in the dash shell, causing all sorts of grief for shell script programmers. This section describes the differences you'll need to be aware of to get your shell scripts to run properly in a dash shell environment.
Creating dash scripts
You probably guessed by now that creating shell scripts for the dash shell is pretty similar to creating shell scripts for the bash shell. You should always specify which shell you want to use in your script to ensure that the script runs with the proper shell.
You do this on the first line of the shell:
#!/bin/dash
You can also specify a shell command line parameter on this line, as was documented earlier in “The dash command line parameters” section.
Things that don't work
Unfortunately, because the dash shell is only a subset of the Bourne shell...
The zsh Shell
Another popular shell that you may run into is the Z shell (called zsh). The zsh shell is an open source Unix shell developed by Paul Falstad. It takes ideas from all the existing shells and adds many unique features to create a full-blown advanced shell designed for programmers.
The following are some of the features that make the zsh shell unique:
- Improved shell option handling
- Shell compatibility modes
- Loadable modules
Of all these features, a loadable module is the most advanced feature in shell design. As you've seen in the bash and dash shells, each shell contains a set of built-in commands that are available without the need for external utility programs. The benefit of built-in commands is execution speed. The shell doesn't have to load a utility program into memory before running it; the built-in commands are already in the shell memory, ready to go.
The zsh shell provides a core set of built-in commands, plus the capability to add more command modules...
Parts of the zsh Shell
This section walks you through the basics of the zsh shell, showing the built-in commands that are available (or can be added by installing modules), as well as the command line parameters and environment variables used by the zsh shell.
Shell options
Most shells use command line parameters to define the behavior of the shell. The zsh shell uses a few command line parameters to define the operation of the shell, but mostly it uses options to customize the behavior of the shell. You can set shell options either on the command line or within the shell itself using the set
command.
Table 23.3 lists the command line parameters available for the zsh shell.
Table 23.3 The zsh Shell Command Line Parameters
Parameter | Description |
-c |
Executes only the specified command and exits |
-i |
Starts as an interactive shell, providing a command line interface prompt |
-s |
Forces the shell to read commands from STDIN |
-o |
Specifies command line options |
Although this may seem...
Scripting with zsh
The main purpose of the zsh shell was to provide an advanced programming environment for shell programmers. With that in mind, it's no surprise that the zsh shell offers many features that make shell scripting easier.
Mathematical operations
As you would expect, the zsh shell allows you to perform mathematical functions with ease. In the past, the Korn shell has led the way in supporting mathematical operations by providing support for floating-point numbers. The zsh shell has full support for floating-point numbers in all its mathematical operations!
Performing calculations
The zsh shell supports two methods for performing mathematical operations:
- The
let
command - Double parentheses
When you use the let
command, you should enclose the operation in double quotation marks to allow for spaces:
% let value1=" 4 * 5.1 / 3.2 "
% echo $value1
6.3750000000
%
Be careful, using floating point numbers may introduce a precision problem. To solve this, it&apos...
Summary
This chapter discussed two popular alternative Linux shells that you may run into. The dash shell was developed as part of the Debian Linux distribution and is mainly found in the Ubuntu Linux distribution. It's a smaller version of the Bourne shell, so it doesn't support as many features as the bash shell, which can cause problems for script writing.
The zsh shell is often found in programming environments, because it provides lots of cool features for shell script programmers. It uses loadable modules to load separate code libraries, which make using advanced functions as easy as running command line commands! There are loadable modules for lots of different functions, from complex mathematical algorithms to network applications such as FTP and HTTP.
The next section of this book dives into some specific scripting applications you might run into in the Linux environment. The next chapter shows how to write simple utilities to help with your day-to-day Linux administration...