trevmex's tumblings

JavaScripter, Rubyist, Functional Programmer, Agile Practitioner.

Add the parent directory to your bash prompt

I am working in a lot of projects lately that have a folder structure like:

<Project Name>/

My current bash prompt shows me the current folder using the \W special character. So my prompt looks something like:

main $ 

But I really want it to look like this since I am often in many projects at once:

<Project Name>/main $

So I created a little bash function to do this in place of \W:

function parent_dir {
    pwd | sed -e "s,.*/\(.*/\),\1,g"

Once you have that in place, all you have to do to add it to your bash prompt is export PS1 like so:

export PS1="\$(parent_dir) $ "

Add the function and the export to your ~/.bashrc file and you will have a more useful prompt!

I hope this helps someone else, too!

The law is the last result of human wisdom acting upon human
experience for the benefit of the public.
Dr. Samuel Johnson
I am a speaker at Agile2014!

I am a speaker at Agile2014!