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
Use Shell OptionsBash 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
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 OptionBash 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 GlobbingThe shell options dotglob, extglob, nocaseglob, and nullglob change the way Bash performs globbing, and as such are covered in Project 12. HistoryProject 48 covers histappend, and Project 49 covers histverify, two shell options that add features to Bash's history mechanism. Use Shell AttributesObtain 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
$ 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 AttributesHere are some useful attributes described by option name, none of which is switched on by default.
If you wish to prevent this from happening, set the noclobber attribute. $ set -o noclobber Learn More
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
$ ls >| letter
Useful Attributes for Shell ScriptsThe following attributes are useful for debugging a shell script and are covered in more detail in Chapter 9.
Enhance Command-Line CompletionThe 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
The readline LibraryBash 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 StyleIf 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 CompletionThe 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 ThresholdIf 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 AmbiguousAfter 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 IndicationIf 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 DifferentTired 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
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 ~
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. |