Customizing Your Shell

HTHM When you use a shell, you're running the shell in an environment that contains ■mm environment variables. Environment variables are predefined values or path­names in various resource text files, found under your home directory and the /etc directory. For the bash shell, the default, systemwide environment variables are defined in the /etc/profile file, whereas personal settings can be found in the file .bashrc in your home directory.

There are many different environment variables. Use the printenv or set command to see a list of the variables currently in use:

# printenv

PATH=/usr/local/bin:/bin:/opt/kde/bin:/usr/bin:.:/usr/X11R6/bin:/home/

^bball/bin

HOME=/home/bball

SHELL=/bin/bash

This hour doesn't list all the environment variables you might find defined for your OpenLinux shell, but one of the most important is the $PATH variable. This variable tells the shell where to find executable programs. Without $PATH, you have to type the com­plete path, or directory hierarchy of a command, to run a program. For example, if you want to run the ifconfig command to check the status of your network connections, you might first try to type its name on the command line:

# ifconfig

bash: ifconfig: command not found

# whereis ifconfig ifconfig: /sbin/ifconfigAs you can see, this doesn't mean that the ifconfig command doesn't exist or isn't installed on your system; it's just that your shell doesn't know where to find the program. To run the ifconfig command, you can type the full pathname before the command, but if you need to use this program repeatedly, include its directory in the list of known paths in your shell's $PATH environment variable.

Do this at the command line by adding the /sbin directory to your $PATH variable, using the bash shell's export command (which saves the new variable definition and makes it available to your shell):

# ifconfig

bash: ifconfig: command not found

# PATH=$PATH:/sbin ; export PATH

# ifconfig

lo Link encap:Local Loopback

inet addr:127.0.0.1   Bcast:127.255.255.255 Mask:255.0.0.0 UP BROADCAST LOOPBACK RUNNING   MTU:3584 Metric:1 RX packets:436 errors:0 dropped:0 overruns:0 TX packets:436 errors:0 dropped:0 overruns:0

As you can see, your shell now knows where to find the ifconfig command. But this is temporary and only lasts as long as you're logged in or running a particular terminal. Make this change effective for each time you log in by adding the path to the file .bash_profile in your home directory or if you're the root operator and want all users to benefit, to the file profile under the /etc directory.

Look for the following line in the .bash_profile file:

PATH=$PATH:$HOME/bin

Using a text editor, add the /sbin directory:

PATH=$PATH:$HOME/bin:/sbin

If you're the root operator, add the /sbin directory in the following line in the profile file under the /etc directory:

PATH="/bin:/sbin:/usr/bin"

After making these changes, read in the new $PATH by using the bash shell's source com­mand, like so:

# source /etc/profile

In the profile file, you also see a line like the following: export PS1="[\u@\h \W]\\$ "Although this line might seem somewhat cryptic, this is the prompt string definition for the $PS1 environment variable and is printed at the command line of your console or ter­minal window when you use a shell. You can change this string to define nearly any type of prompt string. The preceding definition is in the following form:

[username@host base_working_directory]$

The bash shell has 15 different prompts (tcsh has 18, and ksh has 35) you can combine with character strings to customize your prompt. For example, you can have the current date and time in your prompt. If you use the bash shell export command, you can test different prompts from the command line:

# PS1='Date: \d Time: \t-> ';export PS1

Date: Thu Dec 10 Time: 22:19:01->

The time is updated each time you enter a command or press the Enter key. If you want to have the shell's name, along with the current directory in the command line, try the \s and \w prompt characters:

# PS1='\s:\w> '; export PS1

bash:/usr/bin>

You also can use different escape characters and terminal sequences to control the attrib­utes of your prompt strings. You can use underlining, boldfacing, blinking, or other modes for your prompt strings:

# PS1 = '[\033[4m\u\033[0m@\033[4m\h\033[0ni]:';export PS1

[bball@localhost]:

This example uses the proper escape sequences for the xterm X11 terminal (found with the printenv command, and by looking at the $TERMCAP variable) to change the prompt by underlining the username and hostname in the prompt. You can find these definitions included in the file termcap under the /etc directory, and in the termcap manual page. For more examples and other prompt strings for specific shells, consult your shell's manual page.

New Term

Changing the prompt is only one way to customize how you work in your shell. You also can define command shortcuts, or aliases, to tailor how your favorite commands work. You can find at least one or two aliases defined for your system in the bashrc file under the /etc/directory.

Systemwide alias definitions are entered by the root operator. You can put your own defi­nition in the .bashrc file in your home directory, but if you're the root operator, you want to put in at least these three for all your users:

alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'These three aliases cause delete, copy, and rename (move) operations to be done interac­tively and provide at least some element of safety when deleting, copying, or moving (renaming) files. Without the -i, or interactive option, users might not think before delet­ing or overwriting files.

You also can define aliases to build new commands or provide variations of familiar commands to avoid typing long command-line options. For example, the ls command has many different options, but you can define several variations to make life easier. Some alias definitions you might want to try include the following:

# list the current directory using color filenames alias lsc='ls --color1

# long format listing alias lsl='ls -l'

# show all files except . and .. alias lsa='ls -AF'

# show all file except . and .. in color alias lsac='ls -AF --color'

After you enter these changes into your .bashrc file in your home directory, you can use the aliases by again using the bash shell's source command:

# source .bashrc

Now you can type lsc, lsl, lsa, or lsac without adding the command-line options. Just make sure that you don't redefine an existing command! If you come up with some really useful aliases, make sure they're defined for each of the shells active on your sys­tem.

Just because a shell is available on the system does not mean that a user can use a partic­ular shell to log in. As the root operator, you can maintain a list of acceptable shells for your system by editing the file shells under the / etc directory. As a default, the follow­ing shells are listed for OpenLinux:

/bin/bash

/bin/sh (a symbolic link to the bash shell)

/bin/ash

/bin/tcsh

/bin/csh (a symbolic link to the tcsh shell)

/bin/ksh

/bin/zsh

If you don't want your users to be able to use a particular shell, simply remove it from the list! If you try to change your shell with the chsh command to use a particular shell for the next log in, the shell must be listed in the /etc/shells file. The chsh command complains and quits without making any changes:New Term

# chsh -s /bin/zsh

Changing shell for bball. Password:

chsh: "/bin/zsh" is not listed in /etc/shells, chsh: use -l option to see list

Note that this doesn't restrict a user from running a shell after logging in. The only way to effectively manage who can run a particular shell is to change a shell's ownership or file permissions (see Hour 22, "Handling Files and Your File System," for details).