In Bash 4 and later, you can use the special variable PROMPT_DIRTRIM
. The escapes \w
or \W
are available in at least Bash 3.2 and later. These features can be used to control the length of the current directory as displayed in your prompt.
PROMPT_DIRTRIM
If set to a number greater than zero, the value is used as the number of trailing directory components to retain when expanding the \w and \W prompt string escapes (see PROMPTING below). Characters removed are replaced with an ellipsis.\w
the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable)\W
the basename of the current working directory, with $HOME abbreviated with a tilde