FreeUnix.Dyndns.Org Sat, 21 November 2009 - 09:59:08 CET
Home ·  AcpiTool ·  Howto ? ·  Links ·  Hardware ·  FTP Archive ·  Search ·  Contact ·  About
>  Customizing your Bash environment   <
Intro
Most Linux users are running Bash - the Bourne Again Shell - as their shell. This is so because most Linux distro's simply install Bash as their default shell and most people also stick with it. There are other shells available, like csh, zsh, tcsh, ksh . . . etc.
If you want to change your shell, issue the command chsh. This will allow you to change your default shell. However, you can only choose shells listed in /etc/shells. This article will explain briefly how to customize your Bash shell environment.
Files used by Bash
In your home directory, 3 files have a special meaning to Bash, allowing you to set up your environment automatically when you log in and when you invoke another Bash shell, and allow you to execute commands when you log out.
These files may exist in your home directory, but that depends largely on the Linux distro you're using and how your sysadmin (if not you) has set up your account. If they're missing, Bash defaults to /etc/profile.
You can easily create these files yourself using your favorite texteditor. They are:
  • .bash_profile : read and the commands in it executed by Bash every time you log in to the system
  • .bashrc : read and executed by Bash every time you start a subshell
  • .bash_logout : read and executed by Bash every time a login shell exits
Bash allows 2 synonyms for .bash_profile : .bash_login and .profile. These are derived from the C shell's file named .login and from the Bourne shell and Korn shell files named .profile. Only one of these files is read when you log in. If .bash_profile isn't there, Bash will look for .bash_login. If that is missing too, it will look for .profile.
.bash_profile is read and executed only when you start a login shell (that is, when you log in to the system). If you start a subshell (a new shell) by typing bash at the command prompt, it will read commands from .bashrc. This allows you to separate commands needed at login from those needed when invoking a subshell.
However, most people want to have the same commands run regardless of whether it is a login shell or a subshell. This can done by using the source command from within .bash_profile to execute .bashrc. You would then simply place all the commands in .bashrc.
Aliasses
If you have used UNIX for a while, you will know that there are many commands available and that some of them have very cryptic names and/or can be invoked with a truckload of options and arguments. So, it would be nice to have a feature allowing you to rename these commands or type something simple instead of a list of options. Bash provides such a feature : the alias .
Aliasses can be defined on the command line, in .bash_profile, or in .bashrc, using this form :
 alias name=command 
This means that name is an alias for command. Whenever name is typed as a command, Bash will substitute command in its place. Note that there are no spaces on either side of the equal sign. Quotes around command are necessary if the string being aliassed consists of more than one word. A few examples : alias ls='ls -aF --color=always' alias ll='ls -l' alias search=grep alias mcd='mount /mnt/cdrom' alias ucd='umount /mnt/cdrom' alias mc='mc -c' alias ..='cd ..' alias ...='cd ../..'
The first example ensures that ls always uses color if availabe, that dotfiles are listed as well,that directories are marked with a / and executables with a *. To make ls do the same on FreeBSD, the alias would become :
   alias ls='/bin/ls -aFG' 
To see what aliasses are currently active, simply type alias at the command prompt and all active aliasses will be listed. To "disable" an alias type unalias followed by the alias name.
The Bash prompt
Most Linux systems have a default prompt in one colour (usually some boring gray) that tells you your user name, the name of the machine you're working on, and your current working directory. This is all useful information, but you can do much more with the prompt: all sorts of information can be displayed (tty number, time, date, load, number of users, uptime ...) and the prompt can use ANSI colours, either to make it look interesting, or to make certain information stand out.

The appearance of the prompt is defined by the shell variable PS1. Command continuations are indicated by the PS2 string, which can be modified in exactly the same ways discussed here. (There are also PS3 and PS4 strings. These are never seen by the average user - see the Bash man page if you're interested in their purpose.) To change the way the prompt looks, you change the PS1 variable. For experimentation purposes, you can enter the PS1 strings directly at the prompt, and see the results immediately (this only affects your current session, and the changes go away when you log out).

Various people and distributions set their PS? strings in different places. The most common places are /etc/profile, /etc/bashrc, ~/.bash_profile, and ~/.bashrc. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:
 \a     an ASCII bell character (07)
 \d     the date  in  "Weekday  Month  Date"  format
        (e.g., "Tue May 26")
 \e     an ASCII escape character (033)
 \h     the hostname up to the first `.'
 \H     the hostname
 \j     the  number of jobs currently managed by the shell
 \l     the basename of the shell's terminal device name
 \n     newline
 \r     carriage return
 \s     the  name  of  the shell, the basename of $0
        (the portion following the final slash)
 \t     the current time in 24-hour HH:MM:SS format
 \T     the current time in 12-hour HH:MM:SS format
 \@     the current time in 12-hour am/pm format
 \u     the username of the current user
 \v     the version of bash (e.g., 2.00)
 \V     the release of bash,  version  +  patchlevel
        (e.g., 2.00.0)
 \w     the current working directory
 \W     the  basename  of the current working direc­tory
 \!     the history number of this command
 \#     the command number of this command
 \$     if the effective UID is 0, a #, otherwise a $
 \nnn   the character corresponding to the octal number nnn
 \\     a backslash
 \[     begin a sequence of non-printing characters, which could
        be used to embed a terminal control sequence into the prompt
 \]     end a sequence of non-printing characters

And here are the color codes :
 Black       0;30     Dark Gray     1;30
 Blue        0;34     Light Blue    1;34
 Green       0;32     Light Green   1;32
 Cyan        0;36     Light Cyan    1;36
 Red         0;31     Light Red     1;31
 Purple      0;35     Light Purple  1;35
 Brown       0;33     Yellow        1;33
 Light Gray  0;37     White         1;37

Non-printing escape sequences have to be enclosed in \[\033[ and \]. For colour escape sequences, they should also be followed by a lowercase m or 1m for bold. Now you can customize your Bash prompt. I put this in /etc/bashrc (which is sourced from ~/.bashrc) :
 PS1='[\[\033[31;1m\u\[\033[0m@\[\033[34;1m\h\[\033[0m \[\033[34m\w\[\033[0m]>> ' 
The result is this :
bash prompt

Resources
Top