MandrakeUser.Org - Your Mandrake-Linux Knowledge Base!


 
 

* DocIndex - Basics

Using The Shell V

* bash Configuration Files
* The Prompt
* Changing $PATH

Related Resources:

Bash Prompt HOWTO
Path mini-HOWTO
man bash

Revision / Modified: Feb. 28, 2002 / May 17, 2002
Author: Tom Berger

 

* bash Configuration Files

When you do a

ls .bash*

in your home directory, you will see a list of files:

  • '.bash_history' contains a list of the commands you have entered,
  • '.bash_logout' contains a list of commands to be executed when you leave the shell,
  • '.bash_profile' contains a list of commands to be executed when you log in and
  • '.bashrc' contains a list of commands to be executed every time you open a new shell.

Notice that there's a difference between the last two: '.bash_profile' is read once at the beginning of a session, whereas '.bashrc' is readevery time you open a new terminal (e.g. a new xterm window). In a traditional setup you would define variables like PATH in '.bash_profile', and things like aliases and functions in '.bashrc'. But since '.bash_profile' usually is pre-configured to read the content of '.bashrc' anyway, you might as well save some effort and put all your configuration into '.bashrc'.

These files define per user settings. System wide settings are stored in '/etc/profile', '/etc/bashrc' and the files in '/etc/profile.d'. You should prefer using the per user configuration files, though: editing them doesn't require you to be 'root', and they also allow you to set up things differently for 'root' and each user account, which can be a good thing. In case of conflicts between user settings and system settings, user settings prevail.

* section index * top

* The Prompt

The prompt is the first thing you see every time you open a console or an xterm. It looks like this:

account@hostname ~ $

In its default setting it shows your user name, the hostname of your machine (or 'localhost' is you haven't assigned one) and the current working directory ('~' is the Unix shortcut for your home directory).
Traditionally, the last character is set up to indicate whether you are a user ($) or 'root', in which case it changes to a hash (#).

You can set or change your prompt via changing the content of the $PS1 variable. The command

echo $PS1

displays the current setting. Available special characters and their meaning are listed in man bash, section PROMPTING.

Need some ideas on what might be a better prompt? Well, for starters the default setting isn't very friendly to forgetful people, since it only shows the last part of your current path. If you see a prompt like

tom@localhost bin $

your current working directory could be '/bin', '/usr/bin', '/usr/local/bin' or '/usr/X11R6/bin'. Sure, you can type

pwd ('print working directory')

to find out where you are, but shouldn't there be a way to tell the shell to do that on its own accord?
There is. The appearance of the prompt - and for most of the other settings I am going to discuss here - is set in '/etc/bashrc'. You may also change it on a per user basis by editing '.bash_profile' and '.bashrc' in your home directory.

Parameters are described in man bash, chapter 'PROMPTING'. You can add nifty things like the current time in different formats or the history number of the command, even different colors are possible.

My currently favored setting in my user '~/.bashrc' is:

PS1="\[\033[1m\][\w]\[\033[0m\] "

'root's ~/.bashrc has:

PS1="\[\033[0;31m\][\w]\[\033[0m\] "

And the prompt I get, is this:

[/usr/bin]

And this when I'm 'root':

[/usr/bin]

I've chopped the host- and user name part, since I have no need for it. But I want to see at first glance if I am logged in as a user or as 'root' on this console. Note that the user prompt will be white on dark backgrounds and black on light ones.
To get a decent impression of the color capabilities of your terminal, download this script, make it executable (chmod +x color) and run it.

A more moderate setting might be

PS1="\u: \w\\$ "

which will result in prompts like these:

user_name: /usr/bin$

but who cares about being moderate? :-)

You can test various settings on the fly by using the exportcommand (e.g. export PS1="\u: \w\\$ "). If you've found a prompt command which suits you, put it into your '.bashrc'. That way it will be applied automatically to every console or terminal window you open.

You may even 'theme' your prompt, i.e. use different colors or make it look like an good ol' C64 prompt. If you are interested in this, have a look at Bashish.

* section index * top

* Changing $PATH

'$PATH', like '$PS1', belongs to the group of environment variables. Type

set

to get a full list of all currently defined environment variables.
The environment variables you see here are defined in the shell's configuration files, either by the user in his/hers set of shell configuration files or system-wide by 'root' via the shell configuration files in '/etc'. If you are on X, some more variables are set by the start-up files of X and your window manager or desktop environment.

You shouldn't temper with the settings of most these variables unless you know what you are doing and why. Knowing how to change the $PATH variable, however, can be useful, since it determines the names of directories where the shell looks for commands and programs, i.e. for executable files. If the command you want to run is in a directory which is listed in $PATH, you do not need to supply the full path to that command, just the command name. Some third-party software does not install its executable files into the standard Linux directories for commands and programs. Adding their non-standard installation locations to the $PATH is a possible workaround. Furthermore it will also teach you how to deal with environment variables in general.

First off you have noticed that the names of environment variables are written in all capital letters. This is just a convention, but since Linux discriminates between capital and small letters, it is important to keep that in mind. You can define a variable like '$path' or '$pAtH', but the shell won't use it.
The second thing is that variable names are sometimes preceded by an '$', sometimes not. To set a variable, you use its name without a preceding '$':

PATH=/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin

to access the variable and its value, you need to put the '$' in front of it:

echo $PATH
/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin

because otherwise, the variable name is treated as a simple text string:

echo PATH
PATH

The third item is especially important for dealing with the $PATH variable. You can not only replace variables, but also just add new strings to their values. Most of the time, you don't want to do something like 'PATH=/some/directory' because that would erase all the other directories from your $PATH and force you to supply the full path name to everycommand you want to run on that terminal. Instead, just add it:

PATH=$PATH:/some/directory

Here PATH is set to its current value (represented by $PATH), plus one more directory.

So far, you've only set a $PATH variable for the terminal you have typed that command in. If you open a new terminal and issue a echo $PATH, you will see the old $PATH without the directory you just added. You have defined a local environment variable (restricted to the terminal you defined it in).
In order to define a global variable which will be recognized by all terminals you are going to open in this session, you need to export the local variable with the 'export' command:

export PATH=$PATH:/some/directory

If you now open a new terminal and type echo $PATH, you will see the new value of $PATH in this terminal, too. Notice, however, that the 'export' command only sets or changes variables for the terminal it is run in and terminals which are started after it has been run. Terminals which were already open will still have the old $PATH.

In order to add a directory permanently to your $PATH, just add the 'export' command line to your '.bash_profile'.
Do not use '.bashrc' for PATH settings which involve appending directories to the existing PATH. '.bashrc' is sourced every time you open a new shell, so if you append a directory to PATH in it, it will be appended again andevery time you open a terminal. This leads to a PATH variable with an ever growing number of duplicate directory entries.

The last page of this article will introduce two more advanced configuration methods and an FAQ dealing with minor configuration and error message issues.

* section index * top

* Command aliases and shell functions, shell FAQ


 
Legal: All texts on this site are covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB (Tom Berger) and Mandrakesoft 1999-2002.