0

For example, if my home directory is 'FirstLast' and I am inside of

/usr/FirstLast/Dir1/Dir2/Dir3

I want my prompt to read

[username@srv1 Dir1]

regardless of how deep in my directory I am. So the prompt should show 'Dir1' whether I am in Dir2 or Dir3.

I don't want the whole path to show by using pwd and I don't want the current directory.

  • 3
    Can you please clarify exactly what you want? The path you show - /usr/FirstLast/Dir1/Dir2/Dir3 - has nothing to do with your home directory. Do you want your prompt to permanently show something? – ajgringo619 Oct 21 '19 at 23:26
  • I want my prompt to always show the directory after my home directory, if I am in one of the directories inside of my home directory. In my example, I am in Dir3 but the directory immediately after my home directory is Dir1, so that's what I want shown. Even if I am in Dir2, I want the prompt to show Dir1. – David Lopez Oct 22 '19 at 13:43

2 Answers2

1

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 ../../))] '
cas
  • 78,579
  • i just noticed you said I don't want the current directory., so the above won't do what you want (i'd delete it except that it may be useful to other people). How do you expect to be able to define which particular element of the path string should be displayed in the prompt? paths can be of arbitrary length. Always 2 levels above the CWD? – cas Oct 22 '19 at 04:35
  • you'd have to write a shell function (or script) to do what you want, and then call that function inside PS1 - e.g. PS1='[\u@\h $(myfunction)] '. Note1: this will cause the function to be executed every time the prompt is displayed. Note2: the PS1 string has to be enclosed in single-quotes and not double-quotes, otherwise you'll get the return value of the function at the time PS1 was defined. – cas Oct 22 '19 at 04:37
  • Yeah, the arbitrary length of the path is the problem. I guess I was hoping that since 'cd ~' took me to my home directory, perhaps there was a way to keep track of the following directory that I'm currently in (Dir1). I'm fine running the function every time, I'm just not sure how to create it. – David Lopez Oct 22 '19 at 13:27
  • You couldn't do that with a simple call to basename. you'd have to write a function that checked if pwd contained $HOME. If it does, print the next directory in pwd below $HOME. If it doesn't (i.e. you're outside of $HOME, perhaps in /etc or /var or /usr/share/doc or somewhere), do something else (which you haven't defined in your Q - maybe print the full directory). – cas Oct 23 '19 at 00:13
  • personally, i think this kind of thing is a waste of time, and just slows down the shell with pointless calls to functions and external programs. a bit of bling that is briefly satisfying and then ignored 99% of the time. It's simpler and faster just to display the full path with \w or the last element of it it with \W. – cas Oct 23 '19 at 00:16
0

If you in a zsh situation, make the variable of current path to %-2d in PROMPT. That is what you want.

What in my .zshrc:

PROMPT="[username@srv1 %-2d]"