Thursday, October 8, 2009

Linux - Changing Your Prompt String

The prompt is the string that is displayed by Bash when it is waiting for your input. By default your prompt may be some string ending with a $. You can change the prompt. The file .bash_profile contains commands that are executed when you log in to the *nix system. It differs from .bashrc in that the commands in .bash_profile are only executed once, during log in. On the other hand the commands in .bashrc are executed by the shell every time a new shell is started.

Personally, my prompt string is defined in my .bashrc file as PS1='\[\033[1;32m\]\w $\[\033 [0m\] '. The environment variable PS1 is your main prompt string. This funky-looking command sets my prompt to display the current directory (the \w part) followed by a $ in bright green.

Here is a list of all the escape characters you can use:
              \a     ASCII bell character (07)
              \d     Day Month Year
              \e     ASCII escape character (033)
              \h     host base name
              \H     hostname
              \j     # of jobs
              \l     shell terminal device base name
              \n     newline
              \r     carriage return
              \s     shell name
              \t     24 hour HH:MM:SS 
              \T     12 hour HH:MM:SS
              \@     12 hour am/pm time
              \u     current user
              \v     bash version
              \V     bash version + patch
              \w     current directory
              \W     current directory base name
              \!     command history #
              \#     command #
              \$     #(UID 0) or $(UID not 0)
              \nnn   octal nnn(ex \854)
              \\     backslash
              \[     begin of embedded control sequence
              \]     end sequence
 
Example
 
$PS1="[\T][\u][\w]$ " 
 
The prompt string may look like this,
 
[5:45:45][username][~/documents]$ 

To permanently change your prompt in Bash, edit .bashrc and enter a line such as,

PS1=desired-prompt-string; export PS1

You may need to create the .bashrc file in your home directory if it does not exist. After you add that line, your prompt string should permanently be set. If this does not work, try adding the above in your .bash_profile in you home directory(once again, if it does not exit, create it).

No comments:

Post a Comment