Project45.Customize the Bash Shell


Project 45. Customize the Bash Shell

"What tricks can I employ to customize the look and feel of Bash?"

This project looks at shell options and attributes, both of which affect the behavior of Bash. Although attributes change existing behavior, options switch on and off additional (or optional) behavior. The project also shows you how to change the default Bash prompt. It introduces the commands shopt and set.

Tip

Settings shown in this project can be made permanent by writing them to a Bash configuration file. See Project 47.


Use Shell Options

Bash is touched on in many other projects throughout the book and particularly in this chapter. Because the book is task focused rather than theoretical, particular aspects of Bash are dealt with when they coincide with a specific task, such as managing the command-line history in Project 48. Consequently, you'll find that options and attributes appear in other projects, too. This project sweeps up the most useful of the "other miscellaneous" Bash features that don't fit into any other project.

Tip

Read the Bash man page to learn about the available shell options. Type

$ man bash


and then /shopt \[ and press Return to jump straight to the relevant section.

Learn how to use shell options by reading this chapter.


Shell options are viewed and set with the command shopt, whereas shell attributes require the command set. This division can be confusing. The following sections divide options and attributes by functionality.

Use the shopt command with no arguments to display the current setting of each shell option, shown as either on or off.

$ shopt


To display just those shell options that are on, specify option -s (for set); and to display just those that are off, specify option -u (for unset).

Switch on a shell option by typing a command such as

$ shopt -s nameofoption


Switch off a shell option by typing

$ shopt -u nameofoption


Now let's look at some useful shell options.

The cdspell Option

Bash is able to correct minor typos in directory names given to the cd command. It recognizes misspelled names in which you have added or omitted one letter, or transposed a pair of letters. Let's try an example in which we incorrectly type Destop instead of Desktop.

$ cd Destop -bash: cd: Destop: No such file or directory


Now switch on shell option cdspell, and Bash will correct minor typos from now on. Type

$ shopt -s cdspell $ cd Destop Desktop


To switch the option back off, type

$ shopt -u cdspell


Globbing

The shell options dotglob, extglob, nocaseglob, and nullglob change the way Bash performs globbing, and as such are covered in Project 12.

History

Project 48 covers histappend, and Project 49 covers histverify, two shell options that add features to Bash's history mechanism.

Use Shell Attributes

Obtain information on all the shell attributes by typing

$ help set


You'll notice that each attribute is represented by both a single-letter flag, such as -f, and an option name introduced by -o, such as -o noglob . Use either form to switch an attribute on or off. (In fact, a few option names don't have a single-letter equivalent.) Before we look at particular attributes, let's display the current settings. To display those attributes that are currently on, and in single-letter-flag format, type

Tip

Read the Bash man page to learn about the available shell attributes. Type

$ man bash


and then /set \[ and press Return to jump straight to the relevant man section.


$ echo $-


To display attribute status by option name, use either of the following.

$ set -o $ set +o


To switch on an attributefor example, -f or noglob type either of the following.

$ set -f $ set -o noglob


To switch off an attribute, type either of the following.

$ set +f $ set +o noglob


Useful Attributes

Here are some useful attributes described by option name, none of which is switched on by default.

  • Attribute allexport tells Bash to mark automatically all shell variables and functions for export. This attribute causes all new and modified variables to become environment variables and all new and modified functions to be exported.

  • Attribute noclobber stops redirection from overwriting existing files. As an example, assume that we have a file called letter, which we'll create for this example by using the command touch, and then accidentally overwrite it.

    $ touch letter $ ls > letter

If you wish to prevent this from happening, set the noclobber attribute.

$ set -o noclobber


Learn More

Refer to Project 6 to learn about redirection.


Now when we attempt to redirect to an existing file, Bash prevents us from doing so.

$ ls > letter -bash: file: cannot overwrite existing file


If at any time you really do want to overwrite letter, override noclobber by using >| (arrow-pipe) as the redirection symbol.

Tip

Change the default trace prompt by setting the shell variable PS4.


$ ls >| letter


  • Attributes emacs and vi affect command-line editing, changing the mode to either emacs style or vi style. See Project 53 for further information.

  • Attribute xTRace tells Bash to echo each command line before it's executed but after the shell has parsed it and applied all expansions. This attribute is useful as a debugging aid when you're setting up shell variables and aliases.

Useful Attributes for Shell Scripts

The following attributes are useful for debugging a shell script and are covered in more detail in Chapter 9.

  • Attribute noexec dry-runs a script. The script won't be executed, but it will be parsed for syntax errors.

  • Attribute nounset (no unset) tells Bash to raise an error if an unset variable is read.

  • Attribute verbose echoes each command line before it's executed. verbose is like xTRace except that the line is echoed before it's expanded.

Enhance Command-Line Completion

The basics of command-line completionsuch as pressing Tab to complete a filenameare covered in Project 4. This section shows you how to change the default behavior.

Tip

Read the Bash man page to learn about readline. Type

$ man bash


and then /^READLINE and press Return to jump straight to the relevant section.


The readline Library

Bash uses the readline library of functions to receive keyboard input, and readline manages command-line completion on behalf of Bash. To customize completion, we must write the appropriate configuration commands to readline's configuration file, called .inputrc, in your home directory (~/.inputrc).

Here are some of the most useful configuration settings. To try them out, write the lines shown below to ~/.inputrc, and start a new shell.

Bell Style

If you object to the annoying Terminal beep generated whenever filename completion fails, change the bell style to none (be quiet).

set bell-style none


Add the configuration line either by editing .inputrc with one of the Unix text editors covered in Chapter 4, or by appending it directly to the file with the echo command.

$ echo "set bell-style none" >> ~/.inputrc


Other possible arguments to bell-style are visible, to flash the screen, and audible, to ring the Terminal bell. (Terminal's own Window Settings may override readline's settings.)

Case-Insensitive Completion

The HFS+ file system used in Mac OS X is not case sensitive, so it makes sense to ignore case during completion. This allows Documents to be recognized as a completion match when you type lowercase do and press Tab, for example. To do so, type

set completion-ignore-case on


This setting is different from that given in Project 12, where we enabled the shell option nocaseglob. That setting affects how wildcard expansion (which is done by the shell) reacts to case. It does not affect readline's handling of normal completion.

Completion Query Threshold

If the number of matches found for an ambiguous completion attempt exceeds 100, you are prompted with

$ ls /usr/bin/ Display all 717 possibilities? (y or n) ...


To change the threshold value from 100 to, say, 200, use

set completion-query-items 200


Show If Ambiguous

After you press Tab to complete, a second press is required to list possible matches when the completion is ambiguous. Switch on the show-all option to cause completions to be shown immediately after the first Tab press by using

set show-all-if-ambiguous on


Set Visible File Type Indication

If you'd like ambiguous completion to give an indication of file type after each filename displayed, use

set visible-stats on


We now see / after directories and @ after links.

$ ls <Tab> directory-name/ file.txt     link-name@


Think Different

Tired of seeing the same old prompt 1,000 times a day? Would you like to put some color into your life? Then you need a new, improved PS1.

Bash displays the value of shell variable PS1 as the main prompt. To change it, simply change PS1. Type the following for a more subservient prompt.

$ PS1='What now, master? ' What now, master?


Tip

Use command expansion in the prompt. For example, we could display the current date and time by setting PS1 to

$ PS1='$(date) \$ ' Wed Jul 13 13:40:24 BST 2005 $



More usefully, Bash defines several special-character sequences that insert specific information into the prompt. Naturally, we can take advantage of variable expansion in the prompt, too. Here's a more complex example using ANSI codes (see sidebar) to control the color of the text written to the screen. Try this for a blue prompt.

What now, master? PS1='\e[34m\u@\h \w\e[0m\n\$ ' saruman@sauron ~


ANSI Codes

ANSI codes are escape sequences that control output to the Terminal screen. They can move the cursor or change the current foreground and background colors.

Do a "Google" (http://google.com/) for "ANSI Codes" to learn more.


The sequence \e represents Escape and is followed by the ANSI code [34m to set the Terminal foreground color to blue. At the end, we set the color back to black by using Escape [0m. The special characters \u, \h, \w, \n, and \$ are documented in the Bash man page. Search for "PROMPTING" at the start of the line by typing /^PROMPTING within the Bash man page.




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

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