Configuring the Shell


As you saw in the section about aliases, most of us are likely to have our own preferences about how the shell should function. Bash is a highly customizable shell that allows you to set the values of environment variables that change its default behavior. Among other things, users like to change their prompt, their list of aliases, and even perhaps add a welcome message when they log in:

   $ echo $PS1   $   $ export PS1="Grandpoobah > "   Grandpoobah > 

Bash uses the value of the PS1 environment variable to display its prompt. Therefore, you could simply change this environment variable to whatever pleases you. However, to ensure that your brand new prompt is still available to you the next time you log in to the system, you need to add the PS1 setting to the .bashrc file.

Try It Out Modifying the .bashrc File

Add some entries to the .bashrc file (save a backup copy first, so you can put it back to normal when you re done):

   export PS1="Grandpoobah> "     alias ls='ls al'     banner "Good day"   

When you log in, you see a banner that displays the silly Good day message. If you list your aliases and environment variables, you see that your new settings have taken effect.

How it works

When a user logs in, Bash reads the /etc/bashr c file (which is a common startup file for all system users). Then it reads the .bashrc file in the user s home directory and executes all commands in it, including creating aliases, setting up environment variables, and running programs (the banner program in this case).

Because the user s .bashrc is read after the system-wide configuration file, this is a good place to override any default settings that may not be to the user s liking.

A user can also create a .bashrc_logout script in his or her home directory, and add programs to it. When the user logs out, Bash reads and executes the commands in the .bashrc_logout file. This is a good location to add that parting message or reminder and simple housekeeping tasks .

A Sample .bashrc

  • Take a look at a sample .bashrc file:

     export PS1='foobar$ ' export PATH=$PATH:/home/deepakt/games alias rm='rm i' alias psc='ps auxww' alias d='date' alias cls='clear' alias jump='cd /home/deepakt/dungeon/maze/labyrinth/deep/down' 

Setting the PS1 environment variable changes the command prompt. We may have a separate directory in which we store a number of games. We also add this directory to the PATH environment variable. In the aliases section, we alias the rm command to itself with the “i option. The “i option forces the rm command to confirm with the user if it is all right to delete a file or directory. This is often a useful setting for novice users to prevent accidental deletion of files or directories. We also abbreviate the ps command and arguments to display the entire command line of processes with the psc alias. The date command is abbreviated as d . Finally, to save on typing the complete path to a deeply nested directory, we create jump , an alias to the cd command that changes our current working directory to the deeply nested directory.

As you saw in the Switching Identities subsection of the Administrative Commands section, the su command switches the identity of a user to that of another user. By default, when the switch happens, the new user s .bashrc file is not executed. However, if you use the option to su , the new user s .bashrc is executed and the current directory is changed to that of the new user:

   $ su  jmillionaire   

Setting the Prompt

You can set the command prompt in several interesting formats. This is possible due to several special character sequences that expand dynamically when assigned to the PS1 variable, as in the following example:

   $ export PS1='\[\w\]'   [/home/deepakt] 

Here, the command prompt is now set to the value of the current directory, /home/deepakt , because the \w character sequence expands to the current working directory.

The following table shows character sequences that may be used to customize the command prompt:

Character Sequence

Description

\d

Today s date

\h

Host name of the system

\s

The name of the current shell

\t

Current time

\u

User s login name

\W

Base name of the current working directory

\w

Current working directory

\!

History number of the current command




Beginning Fedora 2
Beginning Fedora 2
ISBN: 0764569961
EAN: 2147483647
Year: 2006
Pages: 170

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