I'm trying to make PS1 configured as follows:

export PS1="$STARTCOLOR$Green\u@$Purple\h $Red\w$Green>$ENDCOLOR "

And I have some questions.

  • What's the difference between \[\e[32;1m\] and \e[32;1m? Are they the same?
  • After running the export PS1 command, it works well, but when I give the input around 20 characters, the characters are overwritten as I attached. What's wrong with this?
  • What's the meaning of STARTCOLOR(\e[40m)/ENDCOLOR(\e[0m)?


After some tests, I got the following change could solve the problem. That is, the \e format should be replaced by \\[\e format.

Red="\\e[1;31m"    #-- not working
Red="\[\e[1;31m\]" #-- working

3 Answers 3


I have a helper function to set prompt, and because I don't want to spend more time for looking for escape code references, I've coded all text color values into it.

function set_prompt ( )
    # Prompt setup
    # ##################

    # Black            \e[0;30m
    # Blue             \e[0;34m
    # Green            \e[0;32m
    # Cyan             \e[0;36m
    # Red              \e[0;31m
    # Purple           \e[0;35m
    # Brown            \e[0;33m
    # Gray             \e[0;37m
    # Dark Gray        \e[1;30m
    # Light Blue       \e[1;34m
    # Light Green      \e[1;32m
    # Light Cyan       \e[1;36m
    # Light Red        \e[1;31m
    # Light Purple     \e[1;35m
    # Yellow           \e[1;33m
    # White            \e[1;37m

    local COLOR_DEFAULT='\[\e[0m\]'
    local COLOR_BLACK='\[\e[0;30m\]'
    local COLOR_BLUE='\[\e[0;34m\]'
    local COLOR_GREEN='\[\e[0;32m\]'
    local COLOR_CYAN='\[\e[0;36m\]'
    local COLOR_RED='\[\e[0;31m\]'
    local COLOR_PURPLE='\[\e[0;35m\]'
    local COLOR_BROWN='\[\e[0;33m\]'
    local COLOR_GRAY='\[\e[0;37m\]'
    local COLOR_DARK_GRAY='\[\e[1;30m\]'
    local COLOR_L_BLUE='\[\e[1;34m\]'
    local COLOR_L_GREEN='\[\e[1;32m\]'
    local COLOR_L_CYAN='\[\e[1;36m\]'
    local COLOR_L_RED='\[\e[1;31m\]'
    local COLOR_L_PURPLE='\[\e[1;35m\]'
    local COLOR_YELLOW='\[\e[1;33m\]'
    local COLOR_WHITE='\[\e[1;37m\]'

    local PS1_SET_TITLE='\[\e]0;\w\a\]'

    local PS1_SET_TIME="${COLOR_DEFAULT}\t"
    local PS1_SET_RET_CODE="${COLOR_L_RED}(\$?)"
    local PS1_SET_USER="${COLOR_L_GREEN}\u@\h"
    local PS1_SET_PWD="${COLOR_YELLOW}\w"

    local PS1_LN_1="${PS1_SET_TITLE}\n"
    local PS1_LN_2="${PS1_SET_TIME} ${PS1_SET_RET_CODE} "
    local PS1_LN_2="${PS1_LN_2}${PS1_SET_USER} ${PS1_SET_PWD} ${COLOR_DEFAULT}\n"

    echo "${PS1_LN_1}${PS1_LN_2}"

You can then do:

PS1=$( set_prompt )

# Use '#' for root shell
export PS1=${PS1}'$ '

unset -f set_prompt

Here is the link that explains VT100 terminal codes: http://www.termsys.demon.co.uk/vtansi.htm

\[ - begin sequence of non-printing characters
\] - end sequence of non-printing characters

  • What's the difference between "[\e[32;1m]" and \e[32;1m"? Are they the same?

Not the same, and it should be \[\e[32;1m\]'. Without[]` it would try to print the sequence in console.

  • What's the meaning of STARTCOLOR(\e[40m)/ENDCOLOR(\e[0m)?

STARTCOLOR, means set background to black, ENDCOLOR means reset all text attributes, meaning 'give me default console color'

  • +1 Very nice!!! Don't mind I copy this to my bashrc :)
    – Eldelshell
    Commented Sep 8, 2010 at 12:05
  • @Ubersoldat. Not at all. Commented Sep 8, 2010 at 13:14
  • 3
    Use \$ to automatically switch between root and non-root indication. Commented Sep 8, 2010 at 16:36

From the bash manual:

\[    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 \] are not passed to the terminal. They tell bash that the characters between them are not going to be printed. Without them, bash could not know that the sequence following the escape character (e,g. [32;1m) does not take up any space on-screen, which explains why it did not compute the length of the prompt correctly when you left them out.

Note that you haven't been very consistent in your question (or perhaps it's just a mistake with Markdown); you need to have a literal backslash-bracket sequence in $PS1, not just a bracket (which would be displayed literally).

The escape sequences beginning with \e are interpreted by the terminal emulator. They are documented in the Xterm control sequences (ctlseqs) document (other terminal emulators tend to be mostly compatible). For example, \e[32;1m switches to bold and green foreground; \e[40m switches the background color to black; \e[0m restores the default attributes.


Most modern terminal emulators are able to use ANSI escape codes to control various aspects of the display.

Most of the ANSI codes begin with the 2-character code ESC-[ That is the escape character (ASCII decimal 27) followed by the open square bracket character. This sequence is also known as the CSI or Control Sequence Initiator.

Because the escape character is not one you can type directly (the Esc key has other, often application specific, uses) bash uses '\e' to refer to it.

Changing the text colour uses the ANSI Set Graphics Mode command:


where <value> can be a list of values separated by semi-colons (;). Normally just one value is used, although the bold attribute is useful in conjunction with the colour attributes.

Looking at the values listed in Alexander Pogrebnyak's answer, the 0 or 1 before the semi-colon selects bold or not:

\e[1m  # bold
\e[5m  # blink
\e[0m  # all attributes off

There's a useful list of the codes here http://ascii-table.com/ansi-escape-sequences.php

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .