Bash prompt execute command every time a new prompt is displayed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












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 ?










share|improve this question



























    up vote
    6
    down vote

    favorite












    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 ?










    share|improve this question

























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      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 ?










      share|improve this question















      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 ?







      bash prompt






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 3 '16 at 15:30









      Jeff Schaller

      32.6k849110




      32.6k849110










      asked Feb 3 '16 at 15:26









      danielr1996

      185210




      185210




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          9
          down vote













          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)'





          share|improve this answer
















          • 2




            this gives the error bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'
            – danielr1996
            Feb 3 '16 at 15:36






          • 1




            That is really strange. Make a "backup" (oldPS1="$PS1") and then try: PS1='$(git_prompt) '
            – Hauke Laging
            Feb 3 '16 at 15:46

















          up vote
          4
          down vote













          I fixed it now by using this as prompt



          PS1="$greenu $r@ $redh $r: $yellowW !$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






          share|improve this answer
















          • 4




            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
            Sep 21 '16 at 0:28










          • @dthor thank you, it helped
            – Herrgott
            Feb 6 at 9:04

















          up vote
          1
          down vote













          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)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '



          enter image description here






          share|improve this answer






















          • This looks interesting, can you explain what the ps1tm is for?
            – danielr1996
            Feb 3 '16 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
            – Jonas
            Feb 3 '16 at 16:44

















          up vote
          0
          down vote













          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='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='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$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.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f259618%2fbash-prompt-execute-command-every-time-a-new-prompt-is-displayed%23new-answer', 'question_page');

            );

            Post as a guest






























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            9
            down vote













            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)'





            share|improve this answer
















            • 2




              this gives the error bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'
              – danielr1996
              Feb 3 '16 at 15:36






            • 1




              That is really strange. Make a "backup" (oldPS1="$PS1") and then try: PS1='$(git_prompt) '
              – Hauke Laging
              Feb 3 '16 at 15:46














            up vote
            9
            down vote













            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)'





            share|improve this answer
















            • 2




              this gives the error bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'
              – danielr1996
              Feb 3 '16 at 15:36






            • 1




              That is really strange. Make a "backup" (oldPS1="$PS1") and then try: PS1='$(git_prompt) '
              – Hauke Laging
              Feb 3 '16 at 15:46












            up vote
            9
            down vote










            up vote
            9
            down vote









            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)'





            share|improve this answer












            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)'






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 3 '16 at 15:32









            Hauke Laging

            53.9k1282130




            53.9k1282130







            • 2




              this gives the error bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'
              – danielr1996
              Feb 3 '16 at 15:36






            • 1




              That is really strange. Make a "backup" (oldPS1="$PS1") and then try: PS1='$(git_prompt) '
              – Hauke Laging
              Feb 3 '16 at 15:46












            • 2




              this gives the error bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'
              – danielr1996
              Feb 3 '16 at 15:36






            • 1




              That is really strange. Make a "backup" (oldPS1="$PS1") and then try: PS1='$(git_prompt) '
              – Hauke Laging
              Feb 3 '16 at 15:46







            2




            2




            this gives the error bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'
            – danielr1996
            Feb 3 '16 at 15:36




            this gives the error bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'
            – danielr1996
            Feb 3 '16 at 15:36




            1




            1




            That is really strange. Make a "backup" (oldPS1="$PS1") and then try: PS1='$(git_prompt) '
            – Hauke Laging
            Feb 3 '16 at 15:46




            That is really strange. Make a "backup" (oldPS1="$PS1") and then try: PS1='$(git_prompt) '
            – Hauke Laging
            Feb 3 '16 at 15:46












            up vote
            4
            down vote













            I fixed it now by using this as prompt



            PS1="$greenu $r@ $redh $r: $yellowW !$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






            share|improve this answer
















            • 4




              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
              Sep 21 '16 at 0:28










            • @dthor thank you, it helped
              – Herrgott
              Feb 6 at 9:04














            up vote
            4
            down vote













            I fixed it now by using this as prompt



            PS1="$greenu $r@ $redh $r: $yellowW !$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






            share|improve this answer
















            • 4




              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
              Sep 21 '16 at 0:28










            • @dthor thank you, it helped
              – Herrgott
              Feb 6 at 9:04












            up vote
            4
            down vote










            up vote
            4
            down vote









            I fixed it now by using this as prompt



            PS1="$greenu $r@ $redh $r: $yellowW !$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






            share|improve this answer












            I fixed it now by using this as prompt



            PS1="$greenu $r@ $redh $r: $yellowW !$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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 3 '16 at 20:16









            danielr1996

            185210




            185210







            • 4




              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
              Sep 21 '16 at 0:28










            • @dthor thank you, it helped
              – Herrgott
              Feb 6 at 9:04












            • 4




              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
              Sep 21 '16 at 0:28










            • @dthor thank you, it helped
              – Herrgott
              Feb 6 at 9:04







            4




            4




            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
            Sep 21 '16 at 0:28




            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
            Sep 21 '16 at 0:28












            @dthor thank you, it helped
            – Herrgott
            Feb 6 at 9:04




            @dthor thank you, it helped
            – Herrgott
            Feb 6 at 9:04










            up vote
            1
            down vote













            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)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '



            enter image description here






            share|improve this answer






















            • This looks interesting, can you explain what the ps1tm is for?
              – danielr1996
              Feb 3 '16 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
              – Jonas
              Feb 3 '16 at 16:44














            up vote
            1
            down vote













            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)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '



            enter image description here






            share|improve this answer






















            • This looks interesting, can you explain what the ps1tm is for?
              – danielr1996
              Feb 3 '16 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
              – Jonas
              Feb 3 '16 at 16:44












            up vote
            1
            down vote










            up vote
            1
            down vote









            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)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '



            enter image description here






            share|improve this answer














            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)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '



            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 3 '16 at 15:40

























            answered Feb 3 '16 at 15:34









            Jonas

            873515




            873515











            • This looks interesting, can you explain what the ps1tm is for?
              – danielr1996
              Feb 3 '16 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
              – Jonas
              Feb 3 '16 at 16:44
















            • This looks interesting, can you explain what the ps1tm is for?
              – danielr1996
              Feb 3 '16 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
              – Jonas
              Feb 3 '16 at 16:44















            This looks interesting, can you explain what the ps1tm is for?
            – danielr1996
            Feb 3 '16 at 16:37




            This looks interesting, can you explain what the ps1tm is for?
            – danielr1996
            Feb 3 '16 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
            – Jonas
            Feb 3 '16 at 16:44




            @danielr1996 it the text mode (bold unbold blink underline 0..5, in this functions it's set to 01 if no argument exists
            – Jonas
            Feb 3 '16 at 16:44










            up vote
            0
            down vote













            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='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='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$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.






            share|improve this answer
























              up vote
              0
              down vote













              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='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='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$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.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                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='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='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$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.






                share|improve this answer












                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='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='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 3 '16 at 16:05









                glenn jackman

                47.7k265104




                47.7k265104



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f259618%2fbash-prompt-execute-command-every-time-a-new-prompt-is-displayed%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)