Saturday, March 8, 2014

Customizing Your Bash Prompt

In this tutorial I will show you how you can change the look and behavior of the prompt to show more details or use a different coloring style. I will explain how to try all these examples and how to make changes permanently by adding them inside the Bash configuration file. Please note that this stuff is Bash-specific and it won't work in other shells.

Introduction

By default, the prompt may look something like this:



And the command to set such a prompt would be:

PS1='\[\033[0;31m\]$(returncode)\[\033[0;37m\]\[\033[0;35m\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '

In this tutorial I'll try to explain what all these strange characters do by starting with basic examples and going from there. To try the commands in this tutorial, type them in your terminal. If you want to make changes permanently, put the desired line inside the $HOME/.bashrc file.

The $HOME/.bashrc File

To set the prompt permanently instead of only the current session you will have to copy the respective commands inside the $HOME/.bashrc file, which is executed by Bash every time it starts an interactive session.

The $PS1 and $PS2 Environment Variables

PS stands for INS. The $PS1 variable is the one holding the prompt format. The $PS2 variable is used for multi-line commands, and it is usually set to the greater than character (>):



Changing the Look of Your Prompt

Let's start with the simplest example:

export PS1="$ "

As simple as that, your prompt will always look like this:



Now let's add the current user and the machine hostname. We can do this in two ways, both having the same result. Here's the first:

export PS1="$USER@$HOSTNAME$ "

In this case we use the environment variables $USER and $HOSTNAME, which will be expanded into their actual values. The second way would be:

export PS1="\u@\h\\$ "

Here we use the \u escaped character for the user and \h for the hostname, and \\$ which will expand into either $ or # depending on whether you are a normal user or root. Now let's also include the current path:

export PS1="$USER@$HOSTNAME:\w\\$ "

We used \w for the current working directory.

Colors

So now that we know how to change the information our prompt will show us, let's see how we can use colors to customize it. As an example, here is the code for the prompt that I use:

export PS1="\[\033[01;33m\][$USER@$HOSTNAME]\[\033[0;00m\] \[\033[01;32m\]\w$\[\033[0;00m\] "

It looks like this:



Bash uses colored codes for various colors, for example \033[01;32m for foreground green, \033[01;34m for foreground blue or \033[01;41m for background red. You can try various colors by typing in a terminal something like PS1="\033[01;35m$USER@HOST$ ". Notice at the end the code to reset the color formatting after the prompt, so that the commands introduced by the user will be in the default color rather than the prompt color. This code is \[\033[0;00m\].



Additionally, you can create variables inside .bashrc which will hold these codes:

export FGGR="\[\033[01;32m\]" // foreground green export BGRE="\[\033[01;41m\]" // background red # ...

You can now use these variables inside the prompt customization string.

More Examples

I provide some examples on configuring your prompt, along with screenshots for each of them.

Example #1

PS1='\e[1;33;47m\u \e[1;32;47mon \h \e[1;35;47m\d \@\e[0;0m\n\e[1;34m[dir.= \w] \# > \e[0;0m'



0 comments:

Post a Comment