Current directory abbreviation rule in shell prompt

10,593

Solution 1

Another way to shorten the path, if you use \w is with the PROMPT_DIRTRIM shell variable. A demo:

jackman@b7q9bw1:/usr/local/share/doc $ echo "$PS1"
\u@\h:\w \$ 
jackman@b7q9bw1:/usr/local/share/doc $ pwd
/usr/local/share/doc
jackman@b7q9bw1:/usr/local/share/doc $ PROMPT_DIRTRIM=2
jackman@b7q9bw1:.../share/doc $ pwd
/usr/local/share/doc
jackman@b7q9bw1:.../share/doc $ 

Solution 2

What is displayed in the primary prompt is decided by the contents of the PS1 variable. Here is an excerpt from the bash documentation:

\w     the current working  directory,  with  $HOME  abbreviated
       with  a tilde (uses the value of the PROMPT_DIRTRIM vari‐
       able)
\W     the basename of the current working directory, with $HOME
       abbreviated with a tilde

This means that if your PS1 uses \w, then the entire path for the current directory will be printed, whereas \W would cause the other behavior you observed: just the last component is displayed (with an exception, in both cases, for your home directory).

The default PS1 on some random Linux-based system I tried was [\u@\h \W]\$, but this varies from system to system.

Solution 3

This is a matter of what is in your PS1 variable. You can check your PS1 variable by executing echo $PS1.

If there is a \w, you see the full path, if there is a \W, you only see the last folder.

Share:
10,593

Related videos on Youtube

Kenny
Author by

Kenny

Updated on September 18, 2022

Comments

  • Kenny
    Kenny almost 2 years

    Sometimes I see that the current working directory in shell prompt is abbreviated and sometimes not.

    For example /usr/bin will be displayed as bin$ or /folder1/folder2 displayed as folder2$, in other cases I have seen /folder1/folder2 displayed as full /folder1/folder2$

    I am using default terminal settings (I am using Fedora 22 virtual machine for learning, but I also notice this fact in several other tutorial videos using different distro)

    Is there any rule?

    • Kenny
      Kenny over 8 years
      Question updated. I am using default terminal settings of Fedora for learning. Also I notice this behavior in other places using Ubuntu.
    • Svetlin Tonchev
      Svetlin Tonchev over 8 years
      Maybe depends with what user you are browsing through the directories?
    • Jeff Schaller
      Jeff Schaller over 8 years
      Could you add the output of: echo $PS1 ?