5.8. Bash Shell Prompts

The method used to modify the prompt will depend on the shell that is being used. There are several ways to determine which shell is being used. One simple way is to check the environment variable For the shell:

$ echo $SHELL
/usr/local/bin/bash

Once you know which shell you are using, you need to read the man page for that shell. The man page will describe which files are accessed at startup and how the shells will behave depending on how they are invoked. These files may also contain existing prompt statements which can be modified.

5.8.1. Login Shells

The .bash_profile is read during login. No other shell will read this file so things unique to Bash can be placed here.

If an xterm is invoked with xterm -l then it will act as a login shell and will also read the .bash_profile. This is true for all types of terminal windows in XFree86 which are invoked as login shells.

5.8.2. Non-login Shells

The .bashrc is for non-login shells. The .bashrc won't be read for a login shell. Again, things unique to the Bash shell can be placed in this file.

Many window managers for XFree86 will start their shells as non-login types so the .bashrc is read. However, this behavior can be modified if the xterm is invoked with xterm -l and cause the .bash_profile to be read.

5.8.3. Using .profile

The .profile can be used if the same shell is used all the time. Odd things may happen if lines are used that are not recognized by the shell. It may even prevent login in some cases. The .profile is read regardless of the type of shell.

I use a simple variation of a stock prompt. 

[ user@host ] $ 
[ root@host ] #

Code:   
        case $UID in 
                0) PS1="\[\033[1;31m\][ \u@\h ] #\[\033[0m\] ";; 
                *) PS1="\[\033[1;32m\][ \u@\h ] $\[\033[0m\] ";; 
        esac
=========
I use the following two-liner for bash: 

Code:   
     PS1="\u@\h:\l\n\w \j" 
     case `id -u` in 
           0) PS1="${PS1}# ";; 
           *) PS1="${PS1}$ ";; 
     esac   


Producing output like: 


Code:   
user@machine:tty# 
~ 0$    


That \j ("number of jobs currently managed by the shell") is handy if you tend to live in X and launch things from terminal windows.
=========
For bash, the magical incantation 

Code:   
PS1="\u@\h:\W\\$ " 

yields the well-known 

Code:   
<user>&#064;<hostname>:<workdir>$ 
soc&#064;mybox:~$   

prompt. The "\\$" sequence handles different characters ('#' vs. '$') for root- and non-root-users.