bash PS1 arrangement

I'm attempting to make PS1 set up as adheres to.

export PS1="$STARTCOLOR$Green\[email protected]$Purple\h $Red\w$Green>$ENDCOLOR "

And also I have some inquiries.

  • What's the distinction in between" [\ e [32; 1m]" and also \ e [32; 1m"? Are they the very same?
  • After running the 'export PS1' command, it functions well, yet when I offer the input around 20 personalities, the personalities are overwritten as I affixed. What's incorrect with this?
  • What's the definition of STARTCOLOR (\ e [40m)/ ENDCOLOR (\ e [0m)?


After some examinations, I obtained the list below adjustment can address the trouble. That is, the "\ e" layout need to be changed by "\ [\ e" layout.

Red="\\e[1;31m" -- not working
Red="\[\e[1;31m\]" -- working
2019-05-07 01:57:28
Source Share
Answers: 3

Most modern-day incurable emulators have the ability to make use of ANSI run away codes to regulate numerous facets of the display screen.

A lot of the ANSI codes begin with the 2 - personality code ESC - [ That is the retreat personality (ASCII decimal 27) adhered to by the open square brace personality. This series is additionally called the CSI or Control Sequence Initiator.

Due to the fact that the retreat personality is not one you can type straight (the Esc key has various other, usually application details, makes use of) bash makes use of '\ e' to describe it.

Transforming the message colour makes use of the ANSI Set Graphics Mode command :


where <value> can be a checklist of values divided by semi - colons (;). Generally simply one value is made use of, although the vibrant feature serves combined with the colour connects.

Considering the values detailed in Alexander Pogrebnyak's solution, the 0 or 1 prior to the semi - colon picks vibrant or otherwise :

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

There's a valuable checklist of the codes below

2019-05-08 22:44:55

I have an assistant function to set prompt, and also due to the fact that I do not intend to invest even more time for seeking retreat code referrals, I've coded all message shade values right 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}\[email protected]\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 after that do :

PS1=$( set_prompt )

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

unset -f set_prompt

Here is the link that clarifies VT100 incurable codes :

\[ - begin series of non - printing personalities
\] - end series of non - printing personalities

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

Not the very same, and also it needs to be \[\e[32;1m\]'. Without [ ] 'it would certainly attempt to publish the series in console.

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

STARTCOLOR, suggests set history to black, ENDCOLOR suggests reset all message connects, suggesting 'offer me default console shade'

2019-05-08 22:42:15

From the bash guidebook :

\[ begin a series of non - printing personalities, which can be made use of to install a terminal control series right into the prompt
\] end a series of non - printing personalities

\[ and also \] are not passed to the terminal. They inform bash that the personalities in between them are not mosting likely to be published. Without them, bash can not recognize that the series adhering to the retreat personality (e, g. [32;1m) does not occupy any kind of room on - screen, which clarifies why it did not calculate the size of the prompt appropriately when you left them out.

Keep in mind that you have not been really regular in your inquiry (or probably it's simply a blunder with Markdown) ; you require to have an actual backslash - brace series in $PS1, not simply a brace (which would certainly be presented essentially).

The retreat series starting with \e are analyzed by the terminal emulator. They are recorded in the Xterm control sequences (ctlseqs) record (various other terminal emulators often tend to be primarily suitable). As an example, \e[32;1m switches over to vibrant and also environment-friendly foreground ; \e[40m switches over the history shade to black ; \e[0m recovers the default connects.

2019-05-08 22:33:22