2

First of all, I know similar questions have been answered, some of them have even been marked as duplicate. I have read their answers, and while they've given me some insight regarding my problem, I still can't figure out how to fix my PS1. I've also tried reading a bit about UNIX quotes, but that didn't solve my problem either.

Now, into the actual problem: I've had a custom PS1 for a while now, that always worked just fine. It's this one:

SEPARATOR="$WHITEBOLD-"
USER_AND_DOMAIN="$GREENBOLD[$GRAY\u$WHITEBOLD@$GRAY\h$GREENBOLD]"
WORKING_DIR="$GREENBOLD[$YELLOW\w$GREENBOLD]"

export PS1="$USER_AND_DOMAIN$SEPARATOR$WORKING_DIR $WHITEBOLD\t\n$RED>$WHITE "

However, I felt the need to add my current git branch to it, and found a couple solutions in the web. One of them really caught my interest, because it would change the branch name's color depending on whether my working directory was dirty or not. I then started trying to adapt it to my currently existing customization, and at the moment it looks like this:

SEPARATOR="$WHITEBOLD-"
USER_AND_DOMAIN="$GREENBOLD[$GRAY\u$WHITEBOLD@$GRAY\h$GREENBOLD]"
WORKING_DIR="$GREENBOLD[$YELLOW\w$GREENBOLD]"

GIT_BRANCH="$(git branch &>/dev/null;\
if [ $? -eq 0 ]; then \
  echo "$(echo `git status` | grep 'not staged for commit' > /dev/null 2>&1; \
  if [ "$?" -eq "0" ]; then \
    # Changes to working tree
    echo "$SEPARATOR$GREENBOLD[$RED\$(__git_ps1 "%s")$GREENBOLD]"; \    
  else \
    # Clean repository - nothing to commit
    echo "$SEPARATOR$GREENBOLD[$GREEN\$(__git_ps1 "%s")$GREENBOLD]"; \
  fi)"; \
fi)"

export PS1="$USER_AND_DOMAIN$SEPARATOR$WORKING_DIR$GIT_BRANCH $WHITEBOLD\t\n$RED>$WHITE "

However, although it does change the branch name when I navigate to another branch, the branch color doesn't change automatically - it only does so if I re-source the script (it's a separate script under ~/.ps1_setup, which is called within ~/.bashrc) manually. By what I've read, it has to do with the use of single and double quotes, but most examples I've seen are quite simpler than this code, and I've been trying to fiddle with it for the last 2h, getting different results every time, but most of them are messed up prints of the tags in the escaped format.

Again, I know this is somewhat related to stuff that has been asked and answered before, but the answers given were not enough for me to solve this, so I apologize for asking 'again'. Thanks for any help, good folks! :)

Community
  • 1
  • 1
Luca Bezerra
  • 1,160
  • 1
  • 12
  • 23
  • I do not believe you need all those backslashes – IT_User Jan 15 '16 at 18:19
  • You shouldn't have to **echo** your git status. You should also put `git status` in quotes to prevent word splitting. – IT_User Jan 15 '16 at 18:24
  • @bluerojo Thanks for the input! That's actually a modified copy from somewhere else, so since I don't have much knowledge on that and don't know how to do it otherwise, I just left it there :) – Luca Bezerra Jan 15 '16 at 18:54

1 Answers1

2

I'm not a bash expert but I think the problem is that you switched the original's single-quotes to double-quotes, and bash handles these very differently: double-quotes perform variable & command substitutions, whereas single-quotes do not.

Try this instead:

export PS1="$USER_AND_DOMAIN$SEPARATOR$WORKING_DIR"'$(git branch &>/dev/null;\
if [ $? -eq 0 ]; then \
  echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
  if [ "$?" -eq "0" ]; then \
    # @4 - Clean repository - nothing to commit
     echo "'$SEPARATOR$GREENBOLD[$GREEN'"$(__git_ps1 "%s"); \
  else \
    # @5 - Changes to working tree
     echo "'$SEPARATOR$GREENBOLD[$RED'"$(__git_ps1 "%s"); \
  fi)"; \
fi)'"$GREENBOLD] $WHITEBOLD\t\n$RED>$WHITE"
Julius Musseau
  • 4,037
  • 23
  • 27
  • Thanks a lot, mate! Since my `git status` doesn't return any string that contains "nothing to commit" when the working dir is clean, I've changed it to "not staged for commit" and inverted the logic in the `if/else` inner part, so that it expects that string when the WD is dirty instead. Seeing that you've kinda worked on the original code from the link, I'm impressed by how fast you were able to integrate my older PS1 code into it the way I meant to and make it work, hahah. Thanks again! – Luca Bezerra Jan 15 '16 at 18:59