Shell命令行提示定制

/*******************************************************************************
 *                          Shell命令行提示定制
 * 说明:
 *     Shell命令行提示太长也是一种痛苦,只显示当前目录名,或者最后两三个父目录通常
 * 足够了,不必那么长。
 *
 *                                          2019-5-20 深圳 宝安西乡 曾剑锋
 ******************************************************************************/

 1、参考文档
    1. How to display directory in the prompt?
        https://unix.stackexchange.com/questions/448318/how-to-display-directory-in-the-prompt
    2. Show only current and parent directory in bash prompt
        https://unix.stackexchange.com/questions/216953/show-only-current-and-parent-directory-in-bash-prompt
    3. linux shell 字符串操做详解 (长度,读取,替换,截取,链接,对比,删除,位置 )
        https://blog.csdn.net/dongwuming/article/details/50605911

2、处理方法
    1. 修改文件:~/.bashrc
    2. PROMPT_COMMAND: If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).
    3. PROMPT_COMMAND处理方法:
        PROMPT_COMMAND='case $PWD in
                $HOME) HPWD="~";;
                $HOME/*/*) HPWD="${PWD#"${PWD%/*/*}/"}";;       # %表示从后往前剔除,#表示从前日后剔除
                $HOME/*) HPWD="~/${PWD##*/}";;
                /*/*/*) HPWD="${PWD#"${PWD%/*/*}/"}";;
                *) HPWD="$PWD";;
            esac'
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:$HPWD\$'
    4. 只显示当前目录:PS1='${debian_chroot:+($debian_chroot)}\u@\h:\W\$'
相关文章
相关标签/搜索