Minimal and super useful shell prompt 
Tags: shell, bash, linux, terminal
Published: 2021-02-20 11:54:48

Primary shell prompt can be set by setting `PS1` variable in shell's config file. Let's consider bash as our shell.

Setting `PS1` can be as simple as this
```sh
PS1="prompt $"
# prompt $
echo value of PS1 is $PS1
# value of PS1 is prompt $
```
To keep the changes permanent for our every terminal instant, add we will be updating our `.bashrc` file located at `$HOME/.bashrc`

Useful details in a shell prompt could be
- [Current directory](https://dev.to/chhajedji/add-current-working-directory-in-your-shell-prompt-5fca)
- [Current time](https://dev.to/chhajedji/add-time-stamp-in-your-shell-prompt-252c)
- [Background jobs](https://dev.to/chhajedji/background-jobs-add-to-your-shell-prompt-3eg2)
- [Return status of previous command](https://dev.to/chhajedji/add-return-of-last-command-to-your-shell-prompt-58mh).

You can go through above articles to add respective details in the prompt.

If you are someone who likes colors more than text, we have a better option ;) We can differentiate the error and success of command by a different colors. When interested in error code, we can just print it with `echo $?`. I will choose red color when previous command returned error and white when _all is well :)_

For our purpose, we will use 2 colors, bold red and bold white using `tput`
```sh
# Red
bldred="$(tput setaf 1 2>/dev/null)$(tput bold 2>/dev/null)"
# White
bldwht="$(tput setaf 7 2>/dev/null)$(tput bold 2>/dev/null)"
```

And to reset colors to normal:
```sh
# Reset
txtrst="$(tput sgr 0 2>/dev/null)"
```
> [Here](https://github.com/chhajedji/dot-files/blob/master/T480/.bash/colors.sh) are other variables for color codes set using `tput`.

Our final function setting prompt would look something like this:
```sh
# Depth of `$PWD` is decided by this.
export PROMPT_DIRTRIM=2

# `exit_code' Should be first command in `PROMPT_COMMAND' to be
# executed or else return status will always be 0/true (If functions in
# `PROMPT_COMMAND' are written proper :)
[ -n "$PROMPT_COMMAND" ] && PROMPT_COMMAND="exit_code;$PROMPT_COMMAND" ||
    PROMPT_COMMAND="exit_code"

exit_code() {
    EXIT="$?"

    # PS1 needs to be reset or else it will be appended every time to
    # previous one.
    PS1=""

    [ "$EXIT" = "0" ] && EXITCOL=$bldwht || EXITCOL=$bldred

    # This will be final prompt, whatever set earlier will be
    # overwritten by this.
    export PS1="\[\$EXITCOL\]\w\[\033[01;38;5;208m\]\$([ \j -gt 0 ] && echo [\j])\[\$txtrst\]$ "
```
Here we are using `EXITCOL` to set red/white color deciding upon return status and number of background jobs are displayed in orange color.

Setting colors in bash prompt is a bit lengthy process to explain, but in short, you can copy-paste [content of this file](https://github.com/chhajedji/dot-files/blob/master/T480/.bash/colors.sh) in your `.bashrc` and add the required color within `\[$` and `\]`. For example, for regular green, you can use `\[$txtgrn\]`. Also don't forget to reset the text after setting or else all text would appear in the applied color.

By this way of using colors, we can set various colors for different parts of prompt.

Screenshot highlighting different part after setting above prompt:

![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qkefa3em0nvuz7t62nxc.png)

I like this shell prompt as it is minimal prompt displaying only useful information and also not eating up whole terminal space.