|Bash Prompt HOWTO: $Revision: 0.100 $, $Date: 2009/08/07 02:33:22 $|
|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 later in their own section):
> PS1='\033[1;33m>\033[0m ' >
This works fine - until you type in a long 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 (it counts '\033', the ESC character, as one, and all other characters as one each as you'd expect). 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.