Skip to primary content

 

Arabesque

Systems, Tools, and Terminal Science

 

Main menu


About

Bash Guide

FAQ

GNU/Linux Crypto

Nagios Cookbook

Links

Unix as IDE

Vim Kōans

 



Post navigation

     

Prompt directory shortening


Posted on  by 


The common default of some variant of \h:\w\$ for a Bash prompt PS1 string includes the \w escape character, so that the users current working directory appears in the prompt, but with $HOME shortened to a tilde:
tom@sanctum:~$
tom@sanctum:~/Documents$
tom@sanctum:/usr/local/nagios$

This is normally very helpful, particularly if you leave your shell for a time and forget where you are, though of course you can always call the pwd shell builtin. However it can get annoying for very deep directory hierarchies, particularly if youre using a smaller terminal window:
tom@sanctum:/chroot/apache/usr/local/perl/app-library/lib/App/Library/Class:~$

If youre using Bash version 4.0 or above (bash --version), you can save a bit of terminal space by setting the PROMPT_DIRTRIM variable for the shell. This limits the length of the tail end of the \w and \W expansions to that number of path elements:
tom@sanctum:/chroot/apache/usr/local/app-library/lib/App/Library/Class$ PROMPT_DIRTRIM=3
tom@sanctum:.../App/Library/Class$

This is a good thing to include in your ~/.bashrc file if you often find yourself deep in directory trees where the upper end of the hierarchy isnt of immediate interest to you. You can remove the effect again by unsetting the variable:
tom@sanctum:.../App/Library/Class$ unset PROMPT_DIRTRIM
tom@sanctum:/chroot/apache/usr/local/app-library/lib/App/Library/Class$
 



Privacy  Proudly powered by WordPress