14

I have the following prompt in bash which shows the current git branch:

PS1+="$(git_prompt)" #git_prompt is a function in my .bashrc

which works when I source the .bashrc, but not when I change the branch, so the PS1 var gets only evaluated when I source the .bashrc, but it should be evaluated every time a new prompt is displayed. How can this be accomplished with bash 4.3 ?

4 Answers 4

18

I fixed it now by using this as prompt

PS1="$green\u $r@ $red\h $r: $yellow\W \!$r \$(git_prompt) \n$yellow\$ $r"

before I concatenated multiple strings to one PS1 string, there seemed to be the problem. The trick is to write a \ before excuting the command with $(git_prompt).

So $(git_prompt) will be evaluated when the .bashrc is evaluated and

\$(git_prompt) will be evaluated everytime a new prompt is displayed

1
  • 5
    For anyone who finds this in the future: Note the enclosing double-quotes " around the string. Using \$(git_prompt) without those doesn't work.
    – dthor
    Commented Sep 21, 2016 at 0:28
16

Your problem is that $(git_prompt) is evaluated to some constant string before it is added to $PS1. You have to add the code instead:

PS1+='$(git_prompt)'
2
  • 3
    this gives the error bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)' Commented Feb 3, 2016 at 15:36
  • 2
    That is really strange. Make a "backup" (oldPS1="$PS1") and then try: PS1='$(git_prompt) ' Commented Feb 3, 2016 at 15:46
2

try single quote in your ps1

PS1+='$(git_prompt)'

i also suggest my psOne function

psOne () 
{ 
    ps1tm=${1:-01};
    ps1tc=(30 31 32 33 34 35 36 37 38);
    PS1='${debian_chroot:+($debian_chroot)}\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\]\u\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\]@\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\]\h\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\] :\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\] \w\[\033[${ps1tm};${ps1tc[$((RANDOM%${#ps1tc[@]}))]}m\] \$ '
}

enter image description here

2
  • This looks interesting, can you explain what the ps1tm is for? Commented Feb 3, 2016 at 16:37
  • @danielr1996 it the text mode (bold unbold blink underline {0..5}, in this functions it's set to 01 if no argument exists
    – Yunus
    Commented Feb 3, 2016 at 16:44
1

Want to see madness? This is how I construct my bash prompt:

# inspiration: http://www.stumbleupon.com/su/2LpQMi 
user_host_path="${debian_chroot:+($debian_chroot) }"'\u@\h:\w'
xterm_title='\[\e]0;'"$user_host_path"'\a\]'
[[ $TERM == xterm* || $TERM == rxvt* ]] && line1="${xterm_title}"
git_branch='$(git_current_branch " (%s)")'
line1="${line1}${user_host_path}${git_branch} "
line2='\$ '
print_time='{ printf "%*s" $(($(tput cols) - 10)) " "|sed -e "s/./˙/g" -re "s/.{6}(..)$/ bash \1/"; date "+ %T"; } >&2'
color_bold='\[\e[0;1m\]'
color_reset='\[\e[0m\]'
PROMPT_COMMAND="_rc_=\$?;${print_time};((_rc_!=0)) && PS1='${line1}\n${color_bold}[\$_rc_]${color_reset} ${line2}' || PS1='${line1}\n${line2}'"
unset user_host_path xterm_title color_bold color_reset line1 line2 print_time git_branch

I'm not a big fan of colour.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .