|Bash Prompt HOWTO: $Revision: 0.89 $, $Date: 2001/08/22 00:57:34 $|
|Prev||Chapter 3. Bash Programming and Shell Scripts||Next|
Many of the changes that can be made to Bash prompts that are discussed in this HOWTO use non-printing characters. Changing the colour of the prompt text, changing an Xterm title bar, and moving the cursor position all require non-printing characters.
If I want a very simple prompt consisting of a greater-than sign and a space:
[giles@nikola giles]$ PS1='> ' >
This is just a two character prompt. If I modify it so that it's a bright yellow greater-than sign (colours are discussed in their own section):
> PS1='\033[1;33m>\033[0m ' >
This works fine - until you type in a large command line. Because the prompt still only consists of two printing characters (a greater-than sign and a space) but the shell thinks that this prompt is eleven characters long (I think it counts '\033' , '[1' and '[0' as one character each). You can see this by typing a really long command line - you will find that the shell wraps the text before it gets to the edge of the terminal, and in most cases wraps it badly. This is because it's confused about the actual length of the prompt.
So use this instead:
> PS1='\[\033[1;33m\]>\[\033[0m\] '
This is more complex, but it works. Command lines wrap properly. What's been done is to enclose the '\033[1;33m' that starts the yellow colour in '\[' and '\]' which tells the shell "everything between these escaped square brackets, including the brackets themselves, is a non-printing character." The same is done with the '\033[0m' that ends the colour.