|
Configuring the BASH shell
CONFIGURING THE BASH SHELL: VERSION 1.0
Bash is an acronym for Bourne Again SHell. It is compatible with the original
Bourne shell and boasts some enhancments, like command line editing [TRANSLATION: The Bash shell can run program written for the Bourne shell, plus it offers so much more than the Bourne shell]. It is also the default Linux shell and
is the most widely used shell in Linux. For those who do not know, a shell
is a program that acts as an intermediary between the user and the kernel
[TRANSLATION: A shell is a program that takes commands from the
user and passes them into the kernel for processing].
Like most of the
software that comes with Linux, bash is highly configurable.
ASSUMPTIONS
This article makes a few assumptions about you; the reader:
* You know basic Linux commands, eg: ls, cd, mv, etc...
* You know how to use a text editor, eg: vi, emacs, pico, etc...
* You know how to read path names, eg: /etc/profile
* You know a little bash shell programming.
If you are not yet familiar with the above, please read the approrpriate Newbieized
Help File (NHF). While this article can be read without meeting all the said
assumptions, it would benefit you more if you did.
THE CONFIGURATION FILES
bash has five common configuration files. They are not always found on every
Linux distribution, but they are not hard to create. These files are as follows:
* /etc/profile
* /etc/bashrc
* ~/.bash_profile
* ~/.bashrc
* ~/.bash_logout
The files are categorized into two groups: global and local. Global files are
those that contain configurations that affect all users who use bash. Global
files are usually located in /etc. Local files contain personal configurations
for the user, and affect only the specific user who is using them. They are
normally hidden files [TRANSLATION: They start with a '.' like ~/.bashrc]
found in the user's home directory. If you do not have these files, do not worry.
After reading this NHF, you should be able to create your own. For now, an
explanation on each configuration file.
/etc/profile
/etc/profile is the global system configuration for bash which controls the
environmental variables and programs that are to be run when bash is executed
[TRANSLATION: /etc/profile contains variables and programs that are
executed for every user who runs bash]. If you are an MS-DOS user, /etc/profile
is equivalent to autoexec.bat.
/etc/bashrc
/etc/bashrc is the global system configuration for bash which controls the aliases
and functions to be run when bash is executed [TRANSLATION: /etc/bashrc
contains aliases ("shortcuts" to long commands) as well as pieces of code that are
executed when they are called for]. /etc/bashrc is sometimes omitted, and its
contents placed into /etc/profile.
~/.bash_profile
~/.bash_profile is the local system configuration for bash which controls
the environment variables and programs that are run when bash is executed.
These environment variables and functions are specific only to the user, and they
do not affect anyone else. This file is executed right after the global configuration
file /etc/profile is executed [TRANSLATION: Unlike /etc/profile which
affects all users, ~/.bash_profile affects only the user running bash].
~/.bashrc
~/.bashrc is the local system configuration for bash which controls the aliases
and functions to be run when bash is executed. These aliases and functions are
specific only to the user, and they do not affect anyone else. This file is executed
right after the global configuration file /etc/bashrc is executed [TRANSLATION:
Unlike /etc/bashrc which affects all users, ~/.bashrc affects only the user
running bash].
~/.bash_logout
~/.bash_logout is the local system configuration for bash which runs programs
right before the user is logged out. These programs are specific only to the user running
them, and they do not affect anyone else. EXPLAINING
VARIABLES
A variable is a named storage location in the computer's memory. When you define a
variable, this location holds the variable's defined value [TRANSLATION: Think
of a variable as a box called Box A. Whatever you put in Box A, say a ball, is the value
of Box A]. There are two types of variables in bash: environmental variables and
local variables. Environmental variables are those that are created by the system,
and are normally defined in /etc/profile. These variables (which will be explained
later) include SHELL, PS1, PATH, etc... Local variables are those that are defined by
the user, and are usually placed in the local configuration files like ~/.bashrc. These
variables are local only to the user when bash is executed [TRANSLATION:
Environment variables affect everyone who runs bash, while local variables affect
only those who specify them in their local configuration files].
Defining variables
A variable definition consists of three parts. A variable_name, the assignment
operator: "=" and a variable_value. The variable_name is the name of the
variable, and the variable_value is the value to be assigned to the name. Eg:
variable_name=variable_value
Using the box analogy, variable_name is Box A, and variable_value is the ball.
Hence, variable_name's value is a ball. Notice that there are no spaces beside the
assignment operator. When a variable is defined, it has to be made available to
programs the user uses. This is done by exporting the variable with the export command:
export variable_name
Accessing variables
Variables can be accessed by prefixing the variable_name with a dollar sign: "$".
So for example, to view your environment SHELL variable, type the following command:
xconsole$ echo $SHELL
/bin/bash
This means that the variable variable_name is expanded to variable_value when
you prefix it with a "$" symbol. Understanding how to manipulate variables is essential
when configuring bash, as its configuration files are all in bash shell syntax. Now
that the worst is over, we can begin with the actual configuration.
COMMENTING AND UNCOMMENTING
If while editing your configuration files, you find a line of code that you are unsure of,
you can comment it out instead of deleting it. Commenting is the process of "removing"
a particular line by placing special characters in front of it, so that you do not really
delete it from the file. A commented line will not be read by the configuration file, so
it is just as good as deleting it. Here is an example:
FOOBAR=/bin/foobar
To comment this line out so that it will not be read by the configuration file, add the
hash symbol: "#" in front of it:
# FOOBAR=/bin/foobar
If you decide later that you want this line back, just remove the hash symbol. Commenting
is also good for small notes when editing:
# not sure what this variable does, so I decided to
comment it out
# 1 May 1999
# FOOBAR=/bin/foobar
It is a good idea to add useful notes like these so that you will know what you did when
you need to reconfigure your files much later on.
PATH
The first thing to configure is the PATH variable. PATH defines the directories that you have
direct access to. For example; if you have a program called foo in /bin and you want to
run it. If /bin is in your PATH, then all you have to do is to type foo anywhere and the
program would run. If /bin was not in your PATH, you would have to type the full path
name to program foo in order to run it, that is: /bin/foo. This variable is located in
/etc/profile, and it has an unusual syntax. Each directory on the right hand side of
the equation has to be seperated by a colon: ":". Eg:
PATH=/bin:/usr/bin:/usr/local/bin
export PATH
In the above example, three directories, /bin, /usr/bin, and /usr/local/bin are
seperarated by a colon. When creating a new PATH variable, it is important to add the
current PATH variable to the new PATH variable. Eg:
PATH=$PATH:/usr/games
export PATH
This appends /usr/games into the already existing PATH variable, which is $PATH. Recall that
a variable prefixed with a dollar sign is expanded to its original value. In this case, $PATH is
expanded to /bin:/usr/bin:/usr/local/bin. Because it is added to the new PATH variable,
the new PATH variable can now be expanded to /bin:/usr/bin:/usr/local/bin:/usr/games.
If you had omitted $PATH in the definition, then PATH would be expaned to /usr/games. If
you are not root, you may add to and modify your current existing PATH in ~/.bash_profile.
PROMPTS
The PS1 variable is your primary prompt. Depending upon how your system is configured,
the PS1 variable will vary. PS1 is normally defined in /etc/profile, but can be
overridden by defining it again in ~/.bash_profile [TRANSLATION: Because
/etc/profile is only writeable by root, you can only override the environmental
variables there by redefining them in your ~/.bash_profile]. bash recognizes
special characters prefixed with a backslash for the PS1 variable. These characters are:
\t the current time in HH:MM:SS format
\d the date in "Weekday Month Date" forma
t (eg, "Tue May 26")
\n newline
\s the name of the shell
\w the current working directory
\W the basename of the current working directory
\u the username of the current user
\h the hostname
\# the command number of this command
\! the history number of this command
\$ if the effective UID is 0, a #, otherwise a $
These are the characters that allow you to change your prompt. If your PS1 variable
is set as PS1="[\u@h. \W]\$", then your prompt will look like this:
[xconsole@localhost. /etc]$
If you were to change it to the following: PS1="[\t \s]\$ ", you would get:
[12:18:24 bash]$
In addition to these special backslash characters, you can use commands. For
instance, to have your prompt run as a fortune, you can do this:
PS1="`fortune` \$ "
Notice that you have to use "`" instead of "'". This changes PS1 to the following:
He is no laywer who cannot take two sides. $
As you can see, bash is very lenient about the way you configure it, so knock
yourself out. The PS2 variable is your secondary prompt and is used when you
have typed an incomplete command, or when you have typed a backlslash at the
end of a command [TRANSLATION: A backslash at the end of a command
in bash tells it that you are not done with the command. This is when it will
present you with the PS2 variable. bash is also smart enough to know when the
command you are typing is incomplete, and in such a case, it will present you with
the PS2 variable]. When you are presented with the PS2 variable, bash expects
you to finish the command before it will attempt to run it.
|