use \W
(capital W) instead of \w
(lowercase) in your PS1
string. e.g.
PS1='[\u@\h \W] '
From man bash
:
\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
In bash
, you can also set the PROMPT_DIRTRIM
variable:
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.
If you want the prompt to always display the directory two levels above the current directory (or something similarly complicated or unusual), you will need to use command substitution inside the PS1 string.
Note that the PS1 string has to be enclosed in single-quotes, not double-quotes - otherwise you'll get the return value of the command-subst at the time PS1 was defined, rather than it being re-evaluated every time the prompt is displayed.
e.g.
PS1='[\u@\h $(basename $(realpath ../../))] '