DEV Community

chinmay chhajed
chinmay chhajed

Posted on • Updated on

Minimal and super useful shell prompt

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



PS1="prompt $"
# prompt $
echo value of PS1 is $PS1
# value of PS1 is prompt $


Enter fullscreen mode Exit fullscreen mode

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

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



# 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)"


Enter fullscreen mode Exit fullscreen mode

And to reset colors to normal:



# Reset
txtrst="$(tput sgr 0 2>/dev/null)"


Enter fullscreen mode Exit fullscreen mode

Here are other variables for color codes set using tput.

Our final function setting prompt would look something like this:



# 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.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)