Customizing Your Shell Environment


You've seen a few examples already, in both tcsh and bash style, of how to customize the way your shell works through the use of the default suggested settings in the shell config files. These can serve as perfectly valid examples of how to extend your shell's functionality. There are a few extra things you can do on your own, though. Also, some available options aren't clearly demonstrated in the default files. We'll look at how to accomplish these customizations in tcsh and in bash. In most cases, these are built-in shell commands that can be issued either from the command line directly or from within any of the shell configuration files.

Note

Full details on a large number of available built-in commandssome of which appear in csh/tcsh, some of which appear in sh/bash, and some of which appear in bothcan be found in man builtin.


Customizing tcsh

The following sections describe some of the common user-environment enhancements available for tcsh users.

Creating Aliases

The most common shell customization is to create an alias. This customization substitutes the first word of a command with whatever surrogate command string you choose to replace it with. Aliases can greatly simplify your work, especially if there are certain complex commands that you find yourself using on a regular basis. The simplest alias is of the type seen in .cshrc:

alias ll          ls -lA


This alias, as you would expect, replaces a command such as ll /usr/local with ls -lA /usr/local. But what if you want to perform argument substitution as well, reformatting the command's parameters according to what you enter each time? You can do that with the extremely versatile and equally convoluted command-line parsing syntax of tcsh. The man tcsh page explains this in full detail, but for our purposes, simply note that the first argument to a command in an alias assignment can be referred to as \!:1, and further arguments can be indicated as \!:2, \!:3, and so on. Therefore, you can create an alias like the following:

alias lookup   grep \!:1 /etc/passwd


This customization allows you to enter commands such as lookup frank to extract a user's information. The unalias command allows you to remove an alias, as shown here:

unalias lookup


Customizing the Prompt

You can customize your prompt's appearance by setting the prompt shell variable. You can embed the output of any command, for example the hostname or pwd commands, by enclosing them in backticks (as is done in Perl scripts). You can also echo the "command number" variable, which specifies a numbered entry in the shell's editable history with the ! character. The following example is a fairly complex one that incorporates all these tricks, plus the use of sed (a "stream editor" text processor that operates noninteractively on text fed to it at any time) to shorten the output of hostname to its first element:

# set prompt="{`whoami`@`hostname | sed 's/\..*//'`:!} " {root@www:23}


Another use of the prompt shell variable is to reflect the directory you're currently in, which can be echoed using the pwd (present working directory) command:

# set prompt="{`pwd`:!} " {/usr/local/etc:24}


Note that these examples show commands being issued at the shell prompt, so as to show you their immediate effect on the prompt's style; if you incorporate a set prompt command into your .cshrc file, it will take effect every time you log in.

Customizing the Command Path

The command path is the list of locations where tcsh is configured to look for programs whose names you type as commands. Setting the command path in tcsh involves setting the shell variable path (we'll cover shell variables and environment variables in a moment) to an array in which each command directory is listed in a long string within parentheses. You can't add elements to the parenthesized array "on the fly." You have to add the new path element to the string and reissue the entire set command, which is why the path is best set from within one of the config scripts rather than from the command line. The syntax specifies a list of pathnames in parentheses, separated by spaces. This example shows a modified set path statement that adds the hypothetical directory /usr/local/mystuff to your command path:

[View full width]

set path = (/sbin /usr/sbin /bin /usr/bin /usr/local/bin /usr/contrib/bin /usr/X11R6/bin /usr/local/sbin /usr/games . /usr/local/mystuff)


Customizing Character Mappings

Another useful built-in tool is stty. This tool allows you to redefine various character mappings, a practice you might find very useful in cases where your terminal program sends unexpected characters to your programs. This is most commonly seen in confusion between "delete" and "backspace" characters or in line-delimiter characters. The first step is to see what's currently set for the characters:

# stty -a speed 38400 baud; 60 rows; 80 columns; lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl         -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo         -extproc iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk         brkint -inpck -ignpar -parmrk oflags: opost onlcr -ocrnl -oxtabs -onocr -onlret cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow         -dtrflow -mdmbuf cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;         eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;         min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;         stop = ^S; susp = ^Z; time = 0; werase = ^W;


Pay particular attention to the cchars listing, where the key combinations for certain terminal commands are displayed. Now, if you want to set your "erase" character to ^H (Ctrl+H), specify it in just that two-character form:

stty erase ^H


Other tcsh Tips

A useful trick for users of shared systems is what's known as "watch" mode. When you log in, this mechanism will tell you who else is logged in to the system. To do this, use the following sequence in your .cshrc file:

set watch=(1 any any) set who="%n has %a %l from %M."


One thing about tcsh that you'll likely want to disable is the autologout shell variable, which is set by default to 60 minutes. This variable closes your connection if you leave the terminal idle for more than an hour. You can disable autologout easily enough with this config file line:

unset autologout;


Finally, if you set any of these built-in customization variables by editing the configuration scripts, you don't have to log out and back in to incorporate them; you can use the rehash built-in command to force a reread of all the config scripts.

Customizing bash

The following sections describe some of the common user-environment enhancements available for bash users.

Creating Aliases

Aliases in bash work a bit differently from those in tcsh. The alias and substitution text are separated by an equal sign (=) rather than a space or tab. Also, there is no mechanism for performing argument substitution as there is in tcsh, so the lookup alias we created for tcsh can't be created in bash without more extensive gymnastics. Here's a simple bash-style alias, to be placed in your .profile file:

alias ll='ls -laFo'


Customizing the Prompt

Setting your prompt in bash can get interesting. Because bash doesn't implement shell variables in the same parsed way that tcsh does, the best it can really do is to set the "primary" and "secondary" prompts as environment variables (PS1 and PS2), from which the shell reads the strings it displays to you. To get the "email-address-style" prompt you saw earlier in tcsh, use the following code (note the backslash behind the ! character, which doesn't have the same special meaning as it does in tcsh):

# PS1="{`whoami`@`hostname | sed 's/\..*//'`:\!} " {root@www:17}


Similarly, to get a prompt that reflects the present working directory, use the following:

# PS1="{`pwd`:\!} " {/root:18}


Place one of these commands into your .profile file to change your prompt's style for every login session.

Customizing the Command Path

Setting the command path in bash involves setting the PATH environment variable. In this context, it's a colon-separated string of pathnames; in the default .profile file, this string is combined with an export statement to publish the variable into the environment:

[View full width]

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/bin:/usr/X11R6/bin:$HOME/bin; export PATH


To add directories to your path, separate out the commands into three separate lines, as follows:

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/bin: /usr/X11R6/bin:$HOME/bin PATH=$PATH:/usr/local/mystuff export PATH


This technique ensures that the standard, out-of-the-box command path can be kept in sync with changes in the system, and your personal additions to the path can be maintained separately.

The need to export your variables is something we'll talk about in the next section.




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