Why is my function not re-evaluated in PS1?











up vote
8
down vote

favorite












I'm trying to have a part of my prompt set dynamically by a function, so in my .bashrc I have:



asdf ()
{
echo -n $(pwd)
}
PS1="u@h:w $(asdf)$ "


Opening a shell gives me what I expect at first:



$ bash
darthbith@server:~/test /home/darthbith/test$


However, when I change directory, the part defined by the function doesn't change:



darthbith@server:~/test /home/darthbith/test$ cd ~/test2
darthbith@server:~/test2 /home/darthbith/test$


My actual goal is to use the git-prompt.sh script to show the branch of my git repository when I'm in one with pretty colors and everything, but the problem is that it never updates the branch name when I change repositories. The trivial example above is the simplest reproduction I could come up with for my question.



The .bashrc lines that I have to integrate the git-prompt script:



source ~/.git-prompt.sh
PS1="[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$(__git_ps1)$ "









share|improve this question
























  • On a side note, if you want a git prompt I would recommend github.com/magicmonty/bash-git-prompt/blob/master/README.md
    – mgor
    Jul 23 '15 at 18:38















up vote
8
down vote

favorite












I'm trying to have a part of my prompt set dynamically by a function, so in my .bashrc I have:



asdf ()
{
echo -n $(pwd)
}
PS1="u@h:w $(asdf)$ "


Opening a shell gives me what I expect at first:



$ bash
darthbith@server:~/test /home/darthbith/test$


However, when I change directory, the part defined by the function doesn't change:



darthbith@server:~/test /home/darthbith/test$ cd ~/test2
darthbith@server:~/test2 /home/darthbith/test$


My actual goal is to use the git-prompt.sh script to show the branch of my git repository when I'm in one with pretty colors and everything, but the problem is that it never updates the branch name when I change repositories. The trivial example above is the simplest reproduction I could come up with for my question.



The .bashrc lines that I have to integrate the git-prompt script:



source ~/.git-prompt.sh
PS1="[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$(__git_ps1)$ "









share|improve this question
























  • On a side note, if you want a git prompt I would recommend github.com/magicmonty/bash-git-prompt/blob/master/README.md
    – mgor
    Jul 23 '15 at 18:38













up vote
8
down vote

favorite









up vote
8
down vote

favorite











I'm trying to have a part of my prompt set dynamically by a function, so in my .bashrc I have:



asdf ()
{
echo -n $(pwd)
}
PS1="u@h:w $(asdf)$ "


Opening a shell gives me what I expect at first:



$ bash
darthbith@server:~/test /home/darthbith/test$


However, when I change directory, the part defined by the function doesn't change:



darthbith@server:~/test /home/darthbith/test$ cd ~/test2
darthbith@server:~/test2 /home/darthbith/test$


My actual goal is to use the git-prompt.sh script to show the branch of my git repository when I'm in one with pretty colors and everything, but the problem is that it never updates the branch name when I change repositories. The trivial example above is the simplest reproduction I could come up with for my question.



The .bashrc lines that I have to integrate the git-prompt script:



source ~/.git-prompt.sh
PS1="[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$(__git_ps1)$ "









share|improve this question















I'm trying to have a part of my prompt set dynamically by a function, so in my .bashrc I have:



asdf ()
{
echo -n $(pwd)
}
PS1="u@h:w $(asdf)$ "


Opening a shell gives me what I expect at first:



$ bash
darthbith@server:~/test /home/darthbith/test$


However, when I change directory, the part defined by the function doesn't change:



darthbith@server:~/test /home/darthbith/test$ cd ~/test2
darthbith@server:~/test2 /home/darthbith/test$


My actual goal is to use the git-prompt.sh script to show the branch of my git repository when I'm in one with pretty colors and everything, but the problem is that it never updates the branch name when I change repositories. The trivial example above is the simplest reproduction I could come up with for my question.



The .bashrc lines that I have to integrate the git-prompt script:



source ~/.git-prompt.sh
PS1="[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$(__git_ps1)$ "






command-line bash bashrc prompt ps1






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 20:45









wjandrea

7,87342258




7,87342258










asked Jul 23 '15 at 18:28









darthbith

15019




15019












  • On a side note, if you want a git prompt I would recommend github.com/magicmonty/bash-git-prompt/blob/master/README.md
    – mgor
    Jul 23 '15 at 18:38


















  • On a side note, if you want a git prompt I would recommend github.com/magicmonty/bash-git-prompt/blob/master/README.md
    – mgor
    Jul 23 '15 at 18:38
















On a side note, if you want a git prompt I would recommend github.com/magicmonty/bash-git-prompt/blob/master/README.md
– mgor
Jul 23 '15 at 18:38




On a side note, if you want a git prompt I would recommend github.com/magicmonty/bash-git-prompt/blob/master/README.md
– mgor
Jul 23 '15 at 18:38










2 Answers
2






active

oldest

votes

















up vote
14
down vote



accepted










According to Bash prompt Howto:




[21:58:33][giles@nikola:~]$ PS1="[$(date +%H%M)][u@h:w]$ "
[2159][giles@nikola:~]$ ls
bin mail
[2200][giles@nikola:~]$


It's important to notice the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment.







share|improve this answer























  • Thank you! Now if only I could get it to print the colors instead of the escape sequences returned from the function...
    – darthbith
    Jul 23 '15 at 20:23


















up vote
6
down vote













When you used $(..) in double-quotes, the shell evaluated the command substitution before assigning to PS1. Thus, PS1 contained only the output, not the command substitution itself. Instead, either use single-quotes, or escape the $, so that the string is passed as-is to PS1, and then evaluated when the prompt is set:



$ PS1='$(pwd) $ '
/tmp $ cd /var
/var $ echo "$PS1"
$(pwd) $


Compare:



/var $ PS1="$(pwd) $ "
/var $ echo "$PS1"
a /var $ a
/var $





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f651871%2fwhy-is-my-function-not-re-evaluated-in-ps1%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    14
    down vote



    accepted










    According to Bash prompt Howto:




    [21:58:33][giles@nikola:~]$ PS1="[$(date +%H%M)][u@h:w]$ "
    [2159][giles@nikola:~]$ ls
    bin mail
    [2200][giles@nikola:~]$


    It's important to notice the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment.







    share|improve this answer























    • Thank you! Now if only I could get it to print the colors instead of the escape sequences returned from the function...
      – darthbith
      Jul 23 '15 at 20:23















    up vote
    14
    down vote



    accepted










    According to Bash prompt Howto:




    [21:58:33][giles@nikola:~]$ PS1="[$(date +%H%M)][u@h:w]$ "
    [2159][giles@nikola:~]$ ls
    bin mail
    [2200][giles@nikola:~]$


    It's important to notice the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment.







    share|improve this answer























    • Thank you! Now if only I could get it to print the colors instead of the escape sequences returned from the function...
      – darthbith
      Jul 23 '15 at 20:23













    up vote
    14
    down vote



    accepted







    up vote
    14
    down vote



    accepted






    According to Bash prompt Howto:




    [21:58:33][giles@nikola:~]$ PS1="[$(date +%H%M)][u@h:w]$ "
    [2159][giles@nikola:~]$ ls
    bin mail
    [2200][giles@nikola:~]$


    It's important to notice the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment.







    share|improve this answer














    According to Bash prompt Howto:




    [21:58:33][giles@nikola:~]$ PS1="[$(date +%H%M)][u@h:w]$ "
    [2159][giles@nikola:~]$ ls
    bin mail
    [2200][giles@nikola:~]$


    It's important to notice the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 at 20:46









    wjandrea

    7,87342258




    7,87342258










    answered Jul 23 '15 at 18:36









    mgor

    881410




    881410












    • Thank you! Now if only I could get it to print the colors instead of the escape sequences returned from the function...
      – darthbith
      Jul 23 '15 at 20:23


















    • Thank you! Now if only I could get it to print the colors instead of the escape sequences returned from the function...
      – darthbith
      Jul 23 '15 at 20:23
















    Thank you! Now if only I could get it to print the colors instead of the escape sequences returned from the function...
    – darthbith
    Jul 23 '15 at 20:23




    Thank you! Now if only I could get it to print the colors instead of the escape sequences returned from the function...
    – darthbith
    Jul 23 '15 at 20:23












    up vote
    6
    down vote













    When you used $(..) in double-quotes, the shell evaluated the command substitution before assigning to PS1. Thus, PS1 contained only the output, not the command substitution itself. Instead, either use single-quotes, or escape the $, so that the string is passed as-is to PS1, and then evaluated when the prompt is set:



    $ PS1='$(pwd) $ '
    /tmp $ cd /var
    /var $ echo "$PS1"
    $(pwd) $


    Compare:



    /var $ PS1="$(pwd) $ "
    /var $ echo "$PS1"
    a /var $ a
    /var $





    share|improve this answer

























      up vote
      6
      down vote













      When you used $(..) in double-quotes, the shell evaluated the command substitution before assigning to PS1. Thus, PS1 contained only the output, not the command substitution itself. Instead, either use single-quotes, or escape the $, so that the string is passed as-is to PS1, and then evaluated when the prompt is set:



      $ PS1='$(pwd) $ '
      /tmp $ cd /var
      /var $ echo "$PS1"
      $(pwd) $


      Compare:



      /var $ PS1="$(pwd) $ "
      /var $ echo "$PS1"
      a /var $ a
      /var $





      share|improve this answer























        up vote
        6
        down vote










        up vote
        6
        down vote









        When you used $(..) in double-quotes, the shell evaluated the command substitution before assigning to PS1. Thus, PS1 contained only the output, not the command substitution itself. Instead, either use single-quotes, or escape the $, so that the string is passed as-is to PS1, and then evaluated when the prompt is set:



        $ PS1='$(pwd) $ '
        /tmp $ cd /var
        /var $ echo "$PS1"
        $(pwd) $


        Compare:



        /var $ PS1="$(pwd) $ "
        /var $ echo "$PS1"
        a /var $ a
        /var $





        share|improve this answer












        When you used $(..) in double-quotes, the shell evaluated the command substitution before assigning to PS1. Thus, PS1 contained only the output, not the command substitution itself. Instead, either use single-quotes, or escape the $, so that the string is passed as-is to PS1, and then evaluated when the prompt is set:



        $ PS1='$(pwd) $ '
        /tmp $ cd /var
        /var $ echo "$PS1"
        $(pwd) $


        Compare:



        /var $ PS1="$(pwd) $ "
        /var $ echo "$PS1"
        a /var $ a
        /var $






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 23 '15 at 18:34









        muru

        134k19282483




        134k19282483






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f651871%2fwhy-is-my-function-not-re-evaluated-in-ps1%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            數位音樂下載

            When can things happen in Etherscan, such as the picture below?

            格利澤436b