shell 提示中的当前目录缩写规则

Ken*_*nny 6 bash prompt working-directory

有时我看到 shell 提示中的当前工作目录是缩写的,有时不是。

例如/usr/bin将显示为bin$/folder1/folder2显示为folder2$,在其他情况下我看到/folder1/folder2显示为完整/folder1/folder2$

我使用的是默认终端设置(我使用 Fedora 22 虚拟机进行学习,但我也在其他几个使用不同发行版的教程视频中注意到了这一事实)

有什么规律吗?

gle*_*man 12

另一种缩短路径的方法\w是使用PROMPT_DIRTRIMshell 变量。一个演示:

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 $ 
Run Code Online (Sandbox Code Playgroud)


dha*_*hag 7

主要提示中显示的内容由PS1变量的内容决定。以下是 bash 文档的摘录:

\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
Run Code Online (Sandbox Code Playgroud)

这意味着如果您PS1使用\w,则将打印当前目录的整个路径,而\W会导致您观察到的其他行为:只显示最后一个组件(在这两种情况下,您的主目录除外)。

PS1我尝试过的一些基于 Linux 的随机系统上的默认值是 [\u@\h \W]\$,但这因系统而异。