Abbreviated current directory in shell prompt?

Try this:

PROMPT_COMMAND='PS1X=$(perl -pl0 -e "s|^${HOME}|~|;s|([^/])[^/]*/|$""1/|g" <<<${PWD})'

or, pure bash:

PROMPT_COMMAND='PS1X=$(p="${PWD#${HOME}}"; [ "${PWD}" != "${p}" ] && printf "~";IFS=/; for q in ${p:1}; do printf /${q:0:1}; done; printf "${q:1}")'

then

PS1='\u@\h ${PS1X} $ '

produces (notice the ~ for ${HOME}):

rfkrocktk@work-laptop ~/D/P/W/m/s/m/j/c/tkassembled $

I improved my answer thanks to @enzotib's


I like PROMPT_DIRTRIM in bash...

export PROMPT_DIRTRIM=2

will change your example prompt to...

rfkrocktk@work-laptop ../com/tkassembled/ $

It works for me.


Similar to @nicerobot answer, but somewhat shorter:

PROMPT_COMMAND='pwd2=$(sed "s:\([^/]\)[^/]*/:\1/:g" <<<$PWD)'
PS1='\u@\h:$pwd2\$ '

This will show the followin example output:

enzotib@acer:/h/enzotib$ cd
enzotib@acer:/h/enzotib$ cd /usr/share/doc/acpid/examples/
enzotib@acer:/u/s/d/a/examples$

Tags:

Bash

Prompt