Tutorial: Customizing Your Bash Prompt

Posted by Chris7mas on Feb 20, 2014 2:19 PM EDT
TuxArena; By Craciun Dan
Mail this story
Print this story

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.

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='[33[0;31m]$(returncode)[33[0;37m][33[0;35m]${debian_chroot:+($debian_chroot)}[33[01;31m]h[33[01;34m] W $[33[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. For more details on the files which are executed by Bash when it starts read this tutorial. 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="[33[01;33m][$USER@$HOSTNAME][33[0;00m] [33[01;32m]w$[33[0;00m] " It looks like this: Bash uses colored codes for various colors, for example 33[01;32m for foreground green, 33[01;34m for foreground blue or 33[01;41m for background red. You can try various colors by typing in a terminal something like PS1="33[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 [33[0;00m]. Additionally, you can create variables inside .bashrc which will hold these codes:

export FGGR="[33[01;32m]" // foreground green export BGRE="[33[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 Here is an prompt from the ArchWiki, showing more details than the usual prompts: PS1='e[1;33;47mu e[1;32;47mon h e[1;35;47md @e[0;0mne[1;34m[dir.= w] # > e[0;0m' Resources

Bash Guide for Beginners by Machtelt Garrels Customizing the Bash Prompt at Ubuntu Wiki Color Bash Prompt at ArchWiki

Full Story

  Nav
» Read more about: Story Type: Tutorial

« Return to the newswire homepage

This topic does not have any threads posted yet!

You cannot post until you login.