Andrew’s Cool Bash Prompt

Andrew wrote a tip today to have your bash prompt show the last ‘n’ folders of your current directory. His original suggestions is on his website, [Readable Path for Terminal][1].

His code gave me several errors, which I am pretty sure at least one was because of the bash to HTML conversion. So I wanted to get it working so I played around with it and got it to do what Andrew said it should do. Then I modified it to use the same idea as absolute vs relative paths. If it is a truncated path it will not have a preceding / (or ~). It extends on my previous suggestion, [Better Terminal Prompt][2], so show the username in green (as opposed to red for root).

My profile is available as a [text file][file] and shown below:

# Function that returns the last n path components of the specified
# path after replacing $HOME with ~.  Call like:
#     breadcrumbs path n
function breadcrumbs
{
    echo $1 | sed "s|^$HOME|~|" | awk -v n=$2 '{
        # Split the path into components
        count = split($0, components, "/");

        # If there are less than n components, print the whole path
        if (count = n) {
            for (i = count - n + 1; i

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>