Shell prompt that is based on location in filesystem

前端 未结 2 1397
遇见更好的自我
遇见更好的自我 2020-12-10 19:14

I have to work within three main directories under the root filesystem - home/username, project, and scratch. I want my shell prompt to display which of these top level dire

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 19:47

    Please try this method instead and tell us how it works e.g. how your prompt changes in your home directory, your project or scratch directory, and other directories besides those. Tell us what error messages you see as well. The problem lies within it.

    Tell me also how you run it, if it's by script, by direct execution, or through a startup script like ~/.bashrc.

    top_level_dir ()
    {
        __DIR=$PWD
        case "$__DIR" in
        *home*)
            echo home
            ;;
        *scratch*)
            echo scratch
            ;;
        *project*)
            echo project
            ;;
        *)
            echo "$__DIR"
            ;;
        esac
    }
    
    export PS1='$(top_level_dir) : '
    export -f top_level_dir
    

    If it doesn't work, try changing __DIR=$PWD to __DIR=$(pwd) and tell us if it helps too. I also would like to confirm if you're really running bash. Note that there are many variants of sh like bash, zsh, ksh, and dash and the one installed and used by default depends on every system. To confirm that you're using Bash, do echo "$BASH_VERSION" and see if it shows a message.

    You should also make sure that you're running export PS1='$(top_level_dir) : ' with single quotes and not with double quotes: export PS1="$(top_level_dir) : ".

提交回复
热议问题