0

考虑具有任意子目录的某个目录的固定绝对路径:

/full/path/to/fixed/directory
/full/path/to/fixed/directory/first_subdirectory
/full/path/to/fixed/directory/first_subdirectory/second_subdirectory

以及不是上述子目录的任意其他目录directory/other/path

我想在终端中看到以下内容:

user@host: .../directory$ 
user@host: .../directory/first_subdirectory
user@host: .../directory/first_subdirectory/second_subdirectory
user@host: /other/path$ 

最后一行是典型情况,但其他行是

user@host: /full/path/to/fixed/directory$ 
user@host: /full/path/to/fixed/directory/first_subdirectory
user@host: /full/path/to/fixed/directory/first_subdirectory/second_subdirectory

如何实施?

PS 路径可以包含空格。

4

1 回答 1

0

我会使用特殊的PROMPT_COMMANDvar。例如:

[the-old-prompt] $ cat ps1
g_theSpecialDir=/tmp/im/the/magic/directory
g_pwd=

_prompt_cmd()
{
    if [[ $PWD/ == $g_theSpecialDir/* ]]; then
        g_pwd=.../${g_theSpecialDir##*/}${PWD#$g_theSpecialDir}
    else
        g_pwd=$PWD
    fi
}

PROMPT_COMMAND=_prompt_cmd
PS1='[\u@\h $g_pwd] $ '
[the-old-prompt] $ source ./ps1
[whjm@localhost /tmp] $ cd /tmp/im/the/magic/directory/
[whjm@localhost .../directory] $ cd dir1/
[whjm@localhost .../directory/dir1] $ cd dir2/
[whjm@localhost .../directory/dir1/dir2] $ cd /tmp/
[whjm@localhost /tmp] $

您可以在Bash 手册中找到该PROMPT_COMMANDvar 。此解决方案使您可以在提示中包含您喜欢的任何内容。

于 2015-03-01T05:34:57.457 回答