Friday, June 3, 2011

Control your GNU/Bash Prompt

With GNU/Bash, it is easy to control your prompt, defining PS1 (and optionally PS2 to PS4) environment variable.

For better legibility or whatever reason, you may want to
add further information or colors to prompt.
For instance you can see this page, and this one.

But it is very important to escape special characters dedicated to colors.
For instance, this prompt will NOT work:
export PS1="\e[0;32m\t\e[0m \u@\h:\e[0;36m\W\e[0m$ "

With this prompt, GNU/Bash will NOT work properly while breaking lines (for instance if its length if greater than limit) or while moving to begin/end of them.
To work, it is needed to add escaped bracket between each color escaped characters; this is a working prompt with colors (starting with date and time, and then usual information: user name, host and relative path):
export PS1="\[\e[0;32m\]\t\[\e[0m\] \u@\h:\[\e[0;36m\]\W\[\e[0m\]$ "

To persist your GNU/Bash prompt, you can add this in a GNU/Bash startup file (e.g. $HOME/.bashrc).

No comments:

Post a Comment