Shell命令行提示定制

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

 一、参考文档
    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

二、处理方法
    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\$'

 

posted on 2019-05-20 10:34  zengjf  阅读(295)  评论(0编辑  收藏  举报

导航