Is there a way to change the prompt on a different directories on the same machine? For example, I want a simple prompt "[\W] \$" in development directories.

Ideas?

link|improve this question

63% accept rate
1  
It depends on which shell you're using. – Dennis Williamson Jan 1 '11 at 19:09
feedback

1 Answer

up vote 6 down vote accepted

You can put something like this in your .bashrc.

prompt-function() {
    case $PWD in
        /a/dev/dir\
        |/another/dev/dir) 
            PS1="[\W] \$"
            ;;
        *)
            # Change this to the default prompt
            PS1="\$ "
            ;;
     esac
}

PROMPT_COMMAND=prompt-function
link|improve this answer
+1 I would use a case statement instead of a series of elsif statements. You could even put the directories in an array and iterate over them with a for loop. You could use globbing patterns with any of those techniques or you could use regexes with [[ $PWD =~ $pattern ]]. – Dennis Williamson Jan 1 '11 at 19:13
@Dennis: I edited my answer with a case statement. Thanks for the suggestion. – Wuffers Jan 1 '11 at 19:16
If the action is the same, you can list the action once after alternative cases separated by pipe characters. foo|bar) action;; You can put the cases on separate lines by putting a backslash and newline before or after the pipe character. – Dennis Williamson Jan 1 '11 at 21:47
@Dennis: Edited my answer accordingly, again. Thanks for the suggestion! – Wuffers Jan 1 '11 at 21:58
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.