1

I tried to set PS1 in the bash_profile as,

.bash_profile
#!/bin/bash

# .....

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

parse_pwd() {
  basename $PWD
}

PS1=''$(parse_pwd)''$(parse_git_branch)' $ '

Got the expected output: <project-directory-name> (git-branch-name) $

Problem:

When I change the branch or change project then PS1 details are not updating (i.e. neither the branch name nor the project-directory-name). I had to manually run source ~/.bash_profile to see those changes in the terminal.

I googled and tried few suggestions like,

Not sure, but is it something to do with my bash? I am currently using zsh,

$ echo $SHELL
/bin/zsh
Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • 1
    If you are using `zsh`, modifying the `bash` prompt won't do anything. – Lorccan Apr 22 '21 at 10:59
  • 1
    you can try changing your default login shell to bash. It will then start working – Vikash Kumar Apr 22 '21 at 11:02
  • @Amarmath : Why are you fiddling with the bash configuration, if you don't intend to use bash? I would port your definitions over to zsh. While in zsh, the canonical way to do what you want could be made easier (for instance `$(parse_pwd)` would be better written as `$PWD:t` in zsh), at first glance I think that your functions would work in zsh as well. Just put the definitions into `.zshrc`. – user1934428 Apr 22 '21 at 11:08
  • 1
    Check again the first of the two SO answers, and note the difference between a backtick `\`` and a single quote `'`. – Thomas Apr 22 '21 at 11:49
  • I think removing the inner single quotes in your `PS1` variable should work for you – Aserre Apr 22 '21 at 11:59
  • @vikashkumar Changing default shell to **bash** solved the issue. Can you put it as answer so that I can accept the same? – Amarnath Apr 25 '21 at 14:56
  • 1
    @Amarnath I can see the question is closed. Anyway, the purpose of this platform is to learn by helping each other. So, it's fine. – Vikash Kumar Apr 26 '21 at 05:34

1 Answers1

3

If you want to change the prompt for Zsh:

PROMPT="⚡️%{$fg[red]%} ☣︎☯︎⚰︎☤⚚♱♚☨☥☥☨♚♱⚚☤⚰︎☯︎☣︎ %{$fg[red]%}⚡%{$fg[green]%}%T⚡️%{$fg[green]%}%{$fg[yellow]%}%d%{$fg[yellow]%} ⚡️"

^This is just an example of the difference between bash and zsh when setting prompt.^

for your case check this link:

https://www.themoderncoder.com/add-git-branch-information-to-your-zsh-prompt/

or change to bash using:

chsh -s /bin/sh