Bash tips: Customize the shell prompt

The prompt of Bash can be customized, and can displays several useful informations.

The main prompt is stored in the PS1 variable of the shell. You can show it with the following command:

echo $PS1

and you can change it by modifying the content of this variable. For example this command will change your prompt:

PS1="This is my new prompt >>> "

If you close your terminal and open it again, the prompt is reverted to the default ; that is normal because you have not save it. For permanently changing you prompt, you have to modify your ~/.bashrc file. The simple way to change your prompt is to add the following line at the end of the file:

PS1="Prompt >>> "

Now let's see the different things that can be displayed in the prompt…

Prompt Helper

Prompt Helper is a Javascript script that helps you to customize your prompt by displaying your modification in real time. It does not support all the features of a shell, but it is sufficient for doing what we want to.

You need Javascript for using Prompt Helper.

Special sequences

Sequence description Example/Preview
\d The date (+%a\ %b\ %d format) 1) Mon Jun 13
\D{format} The date in the desired format (in strftime format) 2)
\t The time, 24-hour (+%k:%M:%S format) 3) 22:42:01
\T The time, 12-hour (+%l:%M:%S format) 4) 10:42:01
\@ The time, 12-hour, with AM/PM (+%l:%M\ %p format) 5) 10:42 PM or 10:42
\A The time, 24-hour (+%k:%M format) 6) 22:42
\h The host name hostname
\H The full host name (with the domain name) hostname.domain
\j The number of suspended processes in the current shell (<Ctrl>+Z) 0
\l The name of the shell's terminal device tty1 or 1
\s The name of the shell executable bash
\u The current user name username
\v The version of the shell (short) 4.1
\V The version of the shell (with the patch level) 4.1.5
\w The path of the working directory ~/Documents or /usr/bin
\W The name of the working directory Documents or bin
\! The current command number in the history 538
\# The command number (from the start of the shell) 42
\$ If the current user is root, displays a #, else displays a $ $ or #
\[ Start a sequence of non-printing characters
\] End a sequence of non-printing characters
\a
\007
The ASCII bell character
\n Start a new line
\r Carriage return
\\ A single backslash \
\e
\033
The ASCII Escape character. Used by some Control Sequences (see bellow)

Control Sequences (\e, \033)

Here we will see only a part of the formating and color sequences. For using colors in your shell you can use sequences like “\e[Attrm”. Attr can be replaced by one or more attributes (see bellow).

E.g.:

\e[31mRed Text
\e[42mGreen background
\e[1mBold text
\e[1;31;42mBold and Red text, Green background

Colors

Black Red Green Yellow Blue Magenta Cyan White Default color
Foreground 30 31 32 33 34 35 36 37 39
Background 40 41 42 43 44 45 46 47 49

Fore more information about colors see: colors.

Formatting

Bold Underlined Inverted Default formating and color
1 4 7 0

Fore more information about formatting see: formatting.

Example of prompts

Debian default prompt (no color):

${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 

Debian default colored prompt:

${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 

Demo 1:

\[\e]0;[\w]\a\r\e[1;38;5;118;48;5;16m\] \u \[\e[38;5;16;48;5;118m\] \h \[\e[0m\] \$ 

Demo 2:

\n\[\e[0;38;5;118m\]┏━━━━┫\[\e[1;48;5;118;1;38;5;16m\] \u@\h \[\e[0;38;5;118m\]┣━━┫\[\e[1;48;5;118;1;38;5;16m\] \W \[\e[0;38;5;118m\]┣━━━━━\n┃\n┗━━\$❱\[\e[0m\] 
1) , 3) , 4) , 5) , 6)
date command format
2)
Not supported by Prompt Helper

Discussion

Ivan ZavyalovIvan Zavyalov, 2019/07/10 15:03
Pretty cool! Thanks
You could leave a comment if you were logged in.
 
bash/tip_customize_the_shell_prompt.txt · Last modified: 2017/09/01 12:24 (external edit)