Bash prompt customization
Bash when is executed as interactive, uses environment variables to define how the shell prompt looks in different scenarios. One of those variables is PS1, this variable is used to define the prompt displayed when the shell is ready to read a command(this is the one we will normally refer to when we say we would like to customize our prompt).
There are more variables like: PS2 is displayed when the shell needs more input to complete a command, or PS0 that is displayed after it reads a command but before it is executed. But we will only focus on PS1 just now, there is more information on the man page of bash (run the command man bash and search for PS1).
The place holders are:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May
26")
\D{format}
the format is passed to strftime(3) and the result is
inserted into the prompt string; an empty format results
in a locale-specific time representation. The braces are
required
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion
following the final slash)
\t 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
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patch level (e.g., 2.00.0)
\w the current working directory, with $HOME abbreviated
with a tilde (uses the value of the PROMPT_DIRTRIM vari-
able)
\W the basename of the current working directory, with $HOME
abbreviated with a tilde
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could
be used to embed a terminal control sequence into the
prompt
\] end a sequence of non-printing characters
For example, if we wish to display the current user and the base name of the current directory we would set PS1 to:
| |
- Added a greater than symbol as a separator between the prompt and the command input, some users prefer : or $ or %.
If we try that out it looks very plain we could add some separator between the user and the working directory and we could also display some colour.
To display colour on the shell, we use the ANSI scape codes, for example:
| |
With this colour definition, we can then change the color of some parts of the prompt individually:
| |
Note that if we don’t use the $end sequence, all text after will be modified, so to enclose the colour change use the $end color sequence that turns the text back to normal.
Another improvement to the prompt is showing the git status of the current working directory. For this, we will use the PROMPT_COMMAND environment variable, the content of this variable is executed right before the prompt is displayed.
We are going to use two very simple functions, one to check if the current directory is “dirty” on any way, meaning there are any unstaged and/or modifications for git, and we are going to show the branch we are currently on. Here are the two functions:
| |
Then we can call them on our PROMPT_COMMAND variable:
| |
We are using $COLUMNS to obtain the number of columns on the screen so we can display the information on the right-hand side of the screen.
And that’s it, with this as a base we can customize our prompt to give us the information we need and make our development environment more efficient.
PS. my dot files are here