* * *

Without any doubts Linux is the most used operating system in the world, and Terminal or Shell is it’s heart. Writing zippy commands has always been better than dragging the boring mouse pointer all over the screen to get the job done. It’s fast, it’s zippy , it’s geeky, it’s cool.

And to add more to it, linux is customizable in almost every way. You can adapt to make it work like, look like, feel like the way you want.
With this article , which is first in a series of many we shall learn how to customize the prompt of our terminal/shell.
For those who don't know what prompt is, you have to wait as prompt is explained below. For those who still try to figure out what shell is, the wait is less as it is explained in the line just below.

What is Shell ?

A shell is a command-line interpreter  that provides a traditional user interface for the Linux operating system and for Linux-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute or by creating text scripts of one or more such commands.

Linus traditionally has been a CLI based operating system. All the GUI you see in recent distros are nothing but programs running on top of the OS.

Shell is the way you interact with the OS. You type in your commands into the shell which in turn are processed by the OS.

There are various shells available at the present, the ones i am gonna mention here work for Arch Linux, although they may work on other distros as well:

  • BASH

  • DASH

  • KornShell

  • Zsh

  • Elvish

  • Fish

  • Nash

And many others..

What is prompt ?

A command prompt, also referred to simply as a prompt, is a short text message at the start of the command line on a command line interface or shell.



The [heuristic@shubh ~]$  is the prompt that appears before every command.

 

Bash has four prompts that can be customized:

  • PS1 is the primary prompt which is displayed before each command, thus it is the one most people customize.

  • PS2 is the secondary prompt displayed when a command needs more input (e.g. a multi-line command).

  • PS3 is not very commonly used. It is the prompt displayed for Bash's select built-in which displays interactive menus. Unlike the other prompts, it does not expand Bash escape sequences. Usually you would customize it in the script where the select is used rather than in your .bashrc.

  • PS4 is also not commonly used. It is displayed when debugging bash scripts to indicate levels of indirection. The first character is repeated to indicate deeper levels.

Customizing the prompt

 

The value of prompt is saved in a variable name $PS1 (PS2, PS3 and PS4 respectively).

By default, the command prompt is set to [u@h W]$. Each backslash-escaped special characters can be decoded as follows:

  • u : Display the current username.

  • h : Display the hostname

  • W : Print the base of current working directory.

  • $ : Display # (indicates root user) if the effective UID is 0, otherwise display a $.

 

As discussed before, the bash prompt is controlled by a variable named PS1, and we can adjust this variable in your .bashrc file to customize your prompt.

 

In addition, if you want to make these changes available for all system users on the system or globally, all you need to do is modify this variable in the /etc/bash.bashrc file (on Debian and Ubuntu systems) or /etc/bashrc (on other Linux distributions) instead of ~/.bashrc.

Bash allows you to use some shortcuts to retrieve details, such as the user name, the hostname name, the current working directory, date & time etc. These shortcuts are called escape sequences.

Take an example, you want to display the user’s name, hostname, the current directory and the time in 12-hour format followed by $. Then it can be retrieved by modifying the PS1 variable with these escape sequences which displays the required information as below:

  • u : Display the current username.

  • h : Display the hostname

  • W : Print the base of current working directory.

  • @ : Display current time in 12-hour am/pm format


 

Let’s try an example out,

$ export PS1="[\u@\h \W \@]\$"

 

And here is the result,


Notice the new prompt.

There are even more customizations allowed, here is a full list:

 

  • u  Username of the current user,

  • w The current working directory

  • W The last fragment of the current working directory. For example, if you are currently in /home/linodadmin/var, this will give you var.

  • h The name of the computer, upto a dot(.). For example, if your computer is named centos-01.linoxide.com , this gives you centos-01.

  • H FQDN hostname

  • d The date in “Weekday Month Date” format (e.g.”Tue 21 March”)

  • The current time in 24 hour HH:MM:SS format

  • T The current time in 12 hour HH:MM:SS format

  • @ The current time in 12-hour AM/PM format

  • Move on to the next line.

  • ! : the history number of this command

  • # : the command number of this command

  • $ : if the effective UID is 0, a #, otherwise a $

  • j : the number of jobs currently managed by the shell

 

Adding Color to the prompt:

 

Mostly, system admins would like to add some color to their dull shell prompt. This can be achieved with the help of ANSI escape sequences in the PS1 variable. These escape sequences need to be enclosed in [ and ] in order to work properly. In a simple way we can use this command syntax to add colors to the shell prompt.

The basic format of the above color formatting:

'e[x;ym $PS1 e[m'

Where:

  • e[ : Start color scheme.

  • x;y : Color pair to use (x;y)

  • $PS1 : Your shell prompt variable.

  • e[m : Stop color scheme.

Check out the list of color codes which can be used:

txtblk='e[0;30m' # Black - Regular
txtred='e[0;31m' # Red
txtgrn='e[0;32m' # Green
txtylw='e[0;33m' # Yellow
txtblu='e[0;34m' # Blue
txtpur='e[0;35m' # Purple
txtcyn='e[0;36m' # Cyan
txtwht='e[0;37m' # White
bldblk='e[1;30m' # Black - Bold
bldred='e[1;31m' # Red
bldgrn='e[1;32m' # Green
bldylw='e[1;33m' # Yellow
bldblu='e[1;34m' # Blue
bldpur='e[1;35m' # Purple
bldcyn='e[1;36m' # Cyan
bldwht='e[1;37m' # White
unkblk='e[4;30m' # Black - Underline
undred='e[4;31m' # Red
undgrn='e[4;32m' # Green
undylw='e[4;33m' # Yellow
undblu='e[4;34m' # Blue
undpur='e[4;35m' # Purple
undcyn='e[4;36m' # Cyan
undwht='e[4;37m' # White
bakblk='e[40m' # Black - Background
bakred='e[41m' # Red
bakgrn='e[42m' # Green
bakylw='e[43m' # Yellow
bakblu='e[44m' # Blue
bakpur='e[45m' # Purple
bakcyn='e[46m' # Cyan
bakwht='e[47m' # White
txtrst='e[0m' # Text Reset

Let’s try adding colors to our customized prompt.

        $ export PS1="e[0;32m[\u@\h \W \@]e[m\$"

And here is the result


Notice the change in color.

Using a bash script in prompt

 

This is the best part of it all, what if you want your prompt to change according to the scenario. Let me put forth the prompt i am using currently,

It shows an emoji, system name, pwd , time and the command number.
The emoji changes from happy to sad if the last executed command is unsuccessful.

 

I would now suggest you to directly change the values of PS1 in .bashrc file (assuming you are using the bash shell) instead of exporting it.

The.bashrc file generally lies in the home folder for most of the OS, however in case of any doubt you can google a bit.

vim ~.bashrc

You wil find a PS1 = line already in there, just edit that line to,

PS1='`if [ $? -eq 0 ]; then echo "e[0;32m^_^e[m" ;else echo "e[0;31m0_0e[m";fi`e[0;33m[u W ]e[m e[0;34m{@ !}e[m$'

 

And here is the result


You see a green happy emoji showing the last command was successful.



 

And here is one for an unsuccessful attempt.

 

There is one more way to get complex prompts which we shall discuss in the next post.

Till that time keep on playing with linux.
#BASHing