Previous Section  < Day Day Up >  Next Section

10.1. Installing bash as the Standard Shell

As a prelude to system-wide customization, we want to emphasize that bash can be installed as if it were the standard Bourne shell, /bin/sh. Indeed, some systems, such as Linux, come with bash installed instead of the Bourne shell.

If you want to do this with your system, you can just save the original Bourne shell to another filename (in case someone needs to use it) and either install bash as sh in the /bin directory, or better yet install bash in the /bin directory and create a symbolic link from /bin/sh to /bin/bash using the command ln -s /bin/bash /bin/sh. The reason we think that the second option is better is because bash changes its behavior slightly if started as sh, as we will see shortly.

As detailed in Appendix A, bash is backward-compatible with the Bourne shell, except that it doesn't support ^ as a synonym for the pipe character (|). Unless you have an ancient UNIX system, or you have some very, very old shell scripts, you needn't worry about this.

But if you want to be absolutely sure, simply search through all shell scripts in all directories in your PATH. An easy way to perform the search is to use the file command, which we saw in Chapter 5 and Chapter 9. file prints "executable shell script" when given the name of one.[2] Here is a script that looks for ^ in shell scripts in every directory in your PATH:

[2] The exact message varies from system to system; make sure that yours prints this message when given the name of a shell script. If not, just substitute the message your file command prints for "shell script" in the following code.

IFS=:

for d in $PATH; do

    echo checking $d:

    cd $d

    scripts=$(file * | grep 'shell script' | cut -d: -f1)

    for f in $scripts; do

        grep '\^' $f /dev/null

    done

done

The first line of this script makes it possible to use $PATH as an item list in the for loop. For each directory, it cds there and finds all shell scripts by piping the file command into grep and then, to extract the filename only, into cut. Then for each shell script, it searches for the ^ character.[3]

[3] The inclusion of /dev/null in the grep command is a kludge that forces grep to print the names of files that contain a match, even if there is only one such file in a given directory.

If you run this script, you will probably find several occurrences of ^—but these carets should be used within regular expressions in grep, sed, or awk commands, not as pipe characters. As long as carets are never used as pipes, it is safe for you to install bash as /bin/sh.

As we mentioned earlier, if bash is started as sh (because the executable file has been renamed sh or there is a link from sh to bash) its startup behavior will change slightly to mimic the Bourne shell as closely as possible. For login shells it only attempts to read /etc/profile and ~/.profile, ignoring any other startup files like ~/.bash_profile. For interactive shells it won't read the initialization file ~/.bashrc.[4]

[4] bash also enters POSIX mode when started as sh. Versions of bash prior to 2.0 don't—POSIX mode has to be explicitly set with the —posix command-line option.

10.1.1. POSIX Mode

Besides its native operating mode, bash can also be switched into POSIX mode. The POSIX (Portable Operating System Interface) standard, described in detail in Appendix A, defines guidelines for standardizing UNIX. One part of the POSIX standard covers shells.

bash is nearly 100% POSIX-compliant in its native mode. If you want strict POSIX adherence, you can either start bash with the —posix option, or set it from within the shell with set -o posix.

Only in very rare circumstances would you ever have to use POSIX mode. The differences, outlined in Appendix A, are small and are mostly concerned with the command lookup order and how functions are handled. Most bash users should be able to get through life without ever having to use this option.

10.1.2. Command-Line Options

bash has several command-line options that change the behavior of and pass information to the shell. The options fall into two sets: single character options, like we've seen in previous chapters of this book, and multicharacter options, which are a relatively recent improvement to UNIX utilities.[5] Table 10-1 lists all of the options.[6]

[5] Multicharacter options are far more readable and easier to remember than the old, and usually cryptic, single character options. All of the GNU utilities have multicharacter options, but many applications and utilities (certainly those on old UNIX systems) allow only single-character options.

[6] See Appendix B for a list of options for versions of bash prior to 2.0.

Table 10-1. bash command-line options

Option

Meaning

-c string

Commands are read from string, if present. Any arguments after string are interpreted as positional parameters, starting with $0.

-D

A list of all double-quoted strings preceded by $ is printed on the standard ouput. These are the strings that are subject to language translation when the current locale is not C or POSIX. This also turns on the -n option.

-i

Interactive shell. Ignores signals TERM, INT, and QUIT. With job control in effect, TTIN, TTOU, and TSTP are also ignored.

-l

Makes bash act as if invoked as a login shell.

-o option

Takes the same arguments as set -o.

-O, +O shopt-option

shopt-option is one of the shell options accepted by the shopt builtin. If shopt-option is present, -O sets the value of that option; +O unsets it. If shopt-option is not supplied, the names and values of the shell options accepted by shopt are printed on the standard output. If the invocation option is +O, the output is displayed in a format that may be reused as input.

-s

Reads commands from the standard input. If an argument is given to bash, this flag takes precedence (i.e., the argument won't be treated as a script name and standard input will be read).

-r

Restricted shell. See the Section 10.3.1 later in this chapter.

-v

Prints shell input lines as they're read.

-

Signals the end of options and disables further option processing. Any options after this are treated as filenames and arguments. is synonymous with -.

—debugger

Arranges for the debugger profile to be executed before the shell starts. Turns on extended debugging mode and shell function tracing.[7]

—dump-strings

Does the same as -D.

—dump-po-strings

Does the same as -D but the output is in the GNU gettext po (portable object) file format.

—help

Displays a usage message and exits.

—login

Makes bash act as if invoked as a login shell. Same as -l.

—noediting

Does not use the GNU readline library to read command lines if interactive.

—noprofile

Does not read the startup file /etc/profile or any of the personal initialization files.

—norc

Does not read the initialization file ~/.bashrc if the shell is interactive. This is on by default if the shell is invoked as sh.

—posix

Changes the behavior of bash to follow the POSIX guidelines more closely where the default operation of bash is different.

—quiet

Shows no information on shell startup. This is the default.

—rcfile file, —init-file file

Executes commands read from file instead of from the initialization file ~/.bashrc if the shell is interactive.

—verbose

Equivalent to -v.

—version

Shows the version number of this instance of bash and then exits.


[7] Only available in bash version 3.0 and later.

The multicharacter options have to appear on the command line before the single-character options. In addition to these, any set option can be used on the command line. Like shell built-ins, using a + instead of - turns an option off.

Of these options, the most useful are -i (interactive), -r (restricted), -s (read from standard input), -p (privileged), and -m (enable job control). Login shells are usually run with the -i, -s, and -m flags. We'll look at restricted and privileged modes later in this chapter.

    Previous Section  < Day Day Up >  Next Section