How can I get my PS1 prompt to show time, user, host, directories, and Git branch

I’ve seen others add all this information to their PS1 prompt.

I like the approaches that colorize the elements and also do a carriage return at the end.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1


enter image description here

Add this to the .bashrc file

parse_git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/1/'; }
TIME='33[01;31m]t 33[01;32m]'
LOCATION=' 33[01;34m]`pwd | sed "s#(/[^/]{1,}/[^/]{1,}/[^/]{1,}/).*(/[^/]{1,}/[^/]{1,})/{0,1}#1_2#g"`'
BRANCH=' 33[00;33m]$(parse_git_branch)[33[00m]n$ '

This will work on both Ubuntu and OSX.
Note that I have to have the HOST ‘built’ on two lines to show the same way in both Linux and OSX. Didn’t figure out the reason why but it works.

Note the use of the “_” directory which helps prevent long directory nesting from pushing the prompt to 2 lines by only showing top 3 and bottom 3 directories. less than 7 it just shows them all.

Method 2

I use quite a complex PS1 line on my machine. The following set of code creates my PS1 line. Place it in your bashrc file if you wish to use it. I’m not sure if it works on OSX systems, but my guess would be no.

source "/usr/share/git/completion/"




BATT="$(acpi -b | awk '{print $4}' | cut -b1-3)"
FILES_STAT="$(ls -1 | wc -l | sed 's: ::g')"
FILES_SIZE="$(ls -lah | grep -m 1 total | sed 's/1:total //')b"
GIT_PS1='$(__git_ps1 "(%s)")'

if [ $UID -eq 0 ]; then
PS1='[e[0;31m]u[e[m][e[1;37m]@h[e[m] [e[1;34m]W[e[m] [e[1;32m]$[e[m] [e[1;32m]'
elif [ -n "$SSH_CLIENT" ]; then
    PS1='[e[0;31m](SSH)[e[m][e[1;37m]<a href="" class="__cf_email__" data-cfemail="97e2d7">[email protected]</a>h[e[m] [e[1;34m]W[e[m] [e[1;32m]$(acpi -b | awk "{print $4}" | cut -b1-3) $(__git_ps1 "(%s) ")$[e[m] [e[1;32m]'
trap 'echo -ne "e[0m"' DEBUG

I’m currently at work and do not have the above PS1 line set on this machine. I’ll update this answer with a screenshot of how it looks once I get back. But till then let me try to explain what this does:
  1. We source git’s completion script. They have a similar one for zsh in the same location. This gives us the important __git_ps1 function for use in the PS1 line. This script outputs nothing if you’re not in a git repository.
  2. Set some variables to define what kind of information about the git branches is shown on the PS1 line.
  3. I set a few variables to make it easy to manage the huge PS1 line I have. Those escape characters I have draw angluar lines on the console. The SH_* variables are shortvuts for colours. They are ANSI sequences which will cause the terminal to display coloured output. Finally we set some variables which are perform some actions, like get battery status and number of files in current directory.
  4. Here we define the actual PS1 lines. Since I use this setup globally, I first check to see if the current user is root or not. For root, I have a very simple PS1 line which displays the username in red. If you’re connecting via SSH, you don’t want such a complex line and hence, we again display something simpler. Else, display the complete PS1 line. It can take a while to understand this line. It’s a 2-line PS1 which displays a whole lot of information.
  5. The last line, trap ... is required so that the colour settings don’t bleed out into all the output. The last ${SH_GREEN} causes my input to be in green colour. However, I don’t want all the output from all the programs to be coloured green too. Hence, the trap statement.

Method 3


PS1_TIME='33[01;31m]t 33[01;32m]'
parse_git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/1/'; }
PS1_GIT=' [33[00;33m]$(parse_git_branch)[33[00m]]'


How can I get my PS1 prompt to show time, user, host, directories, and Git branch

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments