0

I'm wondering if it's possible to force bash to change it's prompt when I navigate to a certain directory. The specific use case I am looking for is when I navigate into a cloned git repository, I'd like to add which branch I'm working on to the prompt. For example:

user@hostname [~] $ cd git
user@hostname [git] $ cd git-repo #after this, add the branch name
user@hostname [git-repo:develop] $ cd .. #remove branch name as I'm leaving a git repo
user@hostname [git] $

The only thing I've been able to think of is something like this, which I really don't like:

cd_prompt {
  cd "$@"
  if [ -f .prompt ]; then
    source .prompt
  fi
}
alias cd=cd_prompt
ewok
  • 20,148
  • 51
  • 149
  • 254
  • 1
    Hint: use the variable [`PROMPT_COMMAND`](http://www.gnu.org/software/bash/manual/bashref.html#index-PROMPT_005fCOMMAND). – gniourf_gniourf Sep 23 '16 at 14:54
  • see here: http://stackoverflow.com/questions/15883416/adding-git-branch-on-the-bash-command-prompt – euphoria83 Sep 23 '16 at 14:54
  • Arguably duplicative. We already have questions on SO asking how to set a PS1 that includes the current git branch, ie. http://stackoverflow.com/questions/4133904/ps1-line-with-git-current-branch-and-colors – Charles Duffy Sep 23 '16 at 14:55
  • or http://stackoverflow.com/questions/15883416/adding-git-branch-on-the-bash-command-prompt – Charles Duffy Sep 23 '16 at 14:55
  • 1
    btw, `cd $1` is buggy -- look what happens if you're using that with a directory with spaces in its names. Always, always, always quote expansions: `command cd "$1" || return` (the latter piece to stop executing the function and return with a status that reflects the error if `cd` fails). – Charles Duffy Sep 23 '16 at 14:56
  • 1
    @CharlesDuffy: In fact, you'll even want `cd "$@"` instead. (`cd` can usefully be used with zero or more than one arguments). – gniourf_gniourf Sep 23 '16 at 14:57
  • @CharlesDuffy fair point. I'll fix that. – ewok Sep 23 '16 at 14:57
  • 1
    BTW, if one *were* going to go the awful cd-wrapper approach, I'd do it as follows: `cd() { command cd "$@" || return; [ -f .prompt ] && source .prompt; }` -- no need for an alias if one uses `command` to prevent recursion. (ty @gniourf_gniourf for pointing out that `cd`'s argument list length isn't fixed) – Charles Duffy Sep 23 '16 at 14:57
  • @ewok, `cd $@` is still buggy -- it needs to be `cd "$@"`, **with the quotes**. Otherwise you get the exact same behavior as `cd $*`: the list of arguments is converted to a string separated by the first character of `IFS`, and then that string is field-split and glob-expanded back into a list of individual words; in many cases, that new list won't match the original one. – Charles Duffy Sep 23 '16 at 15:00
  • 2
    (BTW, I'd suggest verifying that `.prompt` is owned by either yourself or root before sourcing it -- otherwise, who knows what happens when you `cd /tmp`. Back in the 70s when universities had huge multiuser systems and the penalties for hacking weren't yet draconian, attempted attacks against such sloppiness -- or users with `.` in their `PATH`s -- were commonplace). – Charles Duffy Sep 23 '16 at 15:05
  • @CharlesDuffy also a good point, but at this point we're discussing the best way to implement a terrible idea. – ewok Sep 23 '16 at 15:06
  • I solved this problem in my answer here: https://stackoverflow.com/questions/4133904/ps1-line-with-git-current-branch-and-colors/35282597#35282597 – Dan L Mar 27 '19 at 17:20

0 Answers0