Using The Shell VI
Summary:- bash Configuration Files
- The Prompt
- Changing $PATH
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.
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="PS1="\[\033[1m\][\w]\[\033[0m\] "PS1="\[\033[0;31m\][\w]\[\033[0m\] "
[/usr/bin]
[/usr/bin]
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\\$ "
user_name: /usr/bin$
export
export PS1="\u: \w\\$ "
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 every command 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 above '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 and every 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 a FAQ dealing with minor configuration and error message issues. section index Next Item: Command aliases and shell functions, shell FAQ
Using The Shell VI
Version 1.10 last modified by ptyxs on 08/03/2006 at 08:51
Version 1.10 last modified by ptyxs on 08/03/2006 at 08:51
Document data
- Lost account?
- Join the community, be part of the Club: it's free!
- Get the PWP Download Subscription!
Mandriva.com
Store
Club


