What the prompt?

Usually, you won’t even notice your prompt, as you see it after every command you execute, in each shell you start. The only thing most people notice is that root seems to have a different prompt.

Depending on your distribution, your prompt will look like this:

username@hostname:~>

Or maybe like this:

username@hostname:/home/username/>

root’s prompt might look like this:

hostname:~#

What most people do not know: You can customize that prompt. Simply by setting the variable PS1 how you want it to be…

Showing git information in the bash prompt

In reality, the PS1 variable looks a little different, but your bash fills in the proper information before showing the prompt

${_t}${_u}:\w${_p} 

or

${USER}@${HOST}:${PWD}>

Use the example file to set the prompt. Add the file content to your local ~/.bashrc (or ~/.profile or ~/.bash_profile, as briefly discussed in the last post).

##################################################################################
# Define colors for
#
GREEN='\[\e[01;32m\]'
BLUE='\[\e[01;34m\]'
RED='\[\e[01;31m\]'
VIOLET='\[\e[00;35m\]'
WHITE='\[\e[01;0m\]'

if [ -e /etc/bash_completion.d/git-prompt.sh ]
then

    ##################################################################################
    # source git prompt bash completion file
    #
    source /etc/bash_completion.d/git-prompt.sh

    ##################################################################################
    # show git prompt in git-repositories only
    #
    export GIT_PROMPT_ONLY_IN_REPO=1
 
    ##################################################################################
    # Set prompt variable PS1 using git prompt
    #
    export PS1="${GREEN}\u${WHITE}@${RED}\h${WHITE}:${BLUE}\$(pwd)${VIOLET}\$(__git_ps1 \" (%s)\")${BLUE}>${WHITE}"
    export PS2="> "

else

    ##################################################################################
    # Set prompt variable PS1 without git prompt
    #
    export PS1="${GREEN}\u${WHITE}@${RED}\H${WHITE}:${BLUE}\$(pwd)>${WHITE}"
    export PS2="> "

fi

We start of by defining a color, as this makes the code a lot easier to read (unless you prefer [\e[01;32m\] instead of ${GREEN}…).

Then we need to source the file containing the code that does all the hard work. After that, we decide to only show the git prompt in git repos (makes sense, doesn’t it?). And then, finally we build our own prompt:

export PS1="${GREEN}\u${WHITE}@${RED}\h${WHITE}:${BLUE}\$(pwd)${VIOLET}\$(__git_ps1 \" (%s)\")${BLUE}>${WHITE}"

This then shows the following prompt:

/home/username/path/to/git_aliases_bashrc_and_completion (master)>

In case of modified files a asterisk * is shown after master:

/home/username/path/to/git_aliases_bashrc_and_completion (master *)>

Once the changed file is being added to the index, this turns into a plus sign +:

/home/username/path/to/git_aliases_bashrc_and_completion (master +)>

Customizing the git prompt

There are two things I always add when using a git prompt: showing local modifications, staged files or deletions. And showing whether or not stashes exist. The example file contains the two stanzas that take care of this:

    ##################################################################################
    # Show git state (modified files, ...) in prompt
    #
    export GIT_PS1_SHOWDIRTYSTATE=1

    ##################################################################################
    # Show git stash state in prompt
    #
    export GIT_PS1_SHOWSTASHSTATE=1

There are many things you can do, why not read the excellent documentation? Oh wait, this is pretty short, why not look into the files themselves?

Have fun!

Johannes Kastl
Johannes is a Linux trainer and consultant and has been with B1 Systems since 2017. His topics include configuration management (Ansible, Salt, Chef, Puppet), version control (git), Infrastructure as Code (Terraform) and automation (Jenkins) as well as testing (Inspec, anyone?). At daytime he works as a sysadmin and fixes problems, at night he tries new technologies like Kubernetes (openSUSE Kubic!), podman or transactional-updates.

This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.