如何限制 Linux 终端默认打印的内容(提示)? (即子文件夹的数量)

How to limit what the Linux Teminal prints by defaults (prompt)? (i.e. number of subfolder)

上下文:

问题来自工作文件夹位置:如果我应该在子文件夹的子文件夹的子文件夹中工作等等... Linux 中 shell 的命令行是这么长,可以用两行来正确打印。

问题:

有没有办法只显示最后一个(或最后几个)工作子文件夹?

示例:

实际打印的内容:

user@user-pc:~/Documents/robotic_arm/Monitoring/difference/develop/component/example/subfolder/subexample/module$

我希望看到的内容:

user@user-pc:~/.../subexample/module$

更多信息:

查看 man bash 的提示部分。您通过设置 PS1 配置提示,我怀疑您当前的设置是这样的:

$ echo $PS1
\u@\h:\w$

如果你把它改成

$ PS1='\u@\h:\W$ '

它只会打印当前工作目录的基本名称。

我不知道为什么以前的答案在我的机器上不起作用。但是,另一种有效的解决方案是:

PROMPT_DIRTRIM=N

其中 N 是您要查看的子文件夹的数量。

示例:

user@user-pc:~/Documents/robotic_arm/difference/develop/component/ $ PROMPT_DIRTRIM=2
user@user-pc:~/.../develop/component/ $

以上答案之一提出了解决方案 this question