PS1 with nice PWD

This is my favorite customized bash prompt :

[12:09:44]/Library/.../Documents/dashboard >

Which shows:

  • current time ( which is useful when you want to calculate the execution time)
  • Current working directory, but if it’s more than 30 characters, it would shorten it

This is how my ~/.bash_profile looks for this customization:

export MYPS='$(echo -n "${PWD/#$HOME/~}" | awk -F "/" '"'"'{if (length($0) > 30){ if (NF>4) print $1 "/" $2 "/.../" $(NF-1) "/" $NF; else if (NF>3) print $1 "/" $2 "/.../" $NF; else print $1 "/.../" $NF; } else print $0;}'"'"')'

export PS1="[\$(date +%k:%M:%S)]$(eval 'echo ${MYPS}') > "

And this is actually how it looks:

[12:09:42]/Library/WebServer/Documents >
[12:09:43]/Library/WebServer/Documents > cd dashboard/
[12:09:44]/Library/.../Documents/dashboard >

So if PWD is less than 30 characters it would show it completely, otherwise it would shorten the path which is nice to see where you currently are.


If you are in to colourful prompt and don’t mind multi-line, this is nice too:

export PS1="\[\033[0m\]\[\033[32m\][\$(date +%k:%M:%S)] \
\[\033[33m\]\w \[\033[0m\] \n$"

Which outputs something like this (but colourful):

[16:16:40] /Library/WebServer/Documents

References: This was a very useful thread on SO.


If you want to have your git branch shown in your prompt with a different color, you could change your PS1 to the following:

export PS1="\[\033[0m\]\[\033[32m\][\$(date +%k:%M:%S)] \
\[\033[33m\]\w \[\033[0m\] \
\[\033[1;35m\]\$(__git_ps1 " (%s)")\[\033[0m\] \n$"

and the output is like the following:

[10:47:51] /Library/WebServer/Documents   (master)


To make it clean and clear I found a very nice blog post to define the colors in variables and just use the color names instead of codes.

So now my `.bash_profile` looks like this:

# Reset
Color_Off="\[\033[0m\]"       # Text Reset

# Regular Colors
Black="\[\033[0;30m\]"        # Black
Red="\[\033[0;31m\]"          # Red
Green="\[\033[0;32m\]"        # Green
Yellow="\[\033[0;33m\]"       # Yellow
Blue="\[\033[0;34m\]"         # Blue
Purple="\[\033[0;35m\]"       # Purple
Cyan="\[\033[0;36m\]"         # Cyan
White="\[\033[0;37m\]"        # White

# Bold
BBlack="\[\033[1;30m\]"       # Black
BRed="\[\033[1;31m\]"         # Red
BGreen="\[\033[1;32m\]"       # Green
BYellow="\[\033[1;33m\]"      # Yellow
BBlue="\[\033[1;34m\]"        # Blue
BPurple="\[\033[1;35m\]"      # Purple
BCyan="\[\033[1;36m\]"        # Cyan
BWhite="\[\033[1;37m\]"       # White

export PS1="$BGreen[\$(date +%k:%M:%S)] $Yellow\w $Purple\$(__git_ps1) $Color_Off\n$"


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s