Set last command exit code to bash prompt

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











up vote
2
down vote

favorite












I am running Ubuntu 18.04, and I would like to set my bash prompt so it reads like below:



user:~/Documents [14:22:07] 1 $


My PS1 is as follows:



ALERT_COLOR="$(tput setaf 1)"

# Display unsuccessful exit codes
function exit_status
last_status=$?
if [[ $last_status != 0 ]]; then
echo "$ALERT_COLOR[$last_status]"
fi


parse_git_branch() sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'

if [ "$color_prompt" = yes ]; then
PS1='$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m] [33[36m][t] [$ALERT_COLOR]$exit_status [33[0;37m]$ '
else
PS1='$debian_chroot:+($debian_chroot)u:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt


Everything works apart from the exit code. It never displays the exit code, and if I just do $? in my PS1, then it always shows as 0










share|improve this question









New contributor




Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Welcome to U&L StackExchange! When you say "if I just do $? in my PS1...", how did you do that?
    – JigglyNaga
    38 mins ago






  • 2




    Linking in the already-related: unix.stackexchange.com/questions/8396/…
    – Jeff Schaller
    36 mins ago










  • @JeffSchaller, urgh, the accepted answer there is just wrong
    – ilkkachu
    13 mins ago














up vote
2
down vote

favorite












I am running Ubuntu 18.04, and I would like to set my bash prompt so it reads like below:



user:~/Documents [14:22:07] 1 $


My PS1 is as follows:



ALERT_COLOR="$(tput setaf 1)"

# Display unsuccessful exit codes
function exit_status
last_status=$?
if [[ $last_status != 0 ]]; then
echo "$ALERT_COLOR[$last_status]"
fi


parse_git_branch() sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'

if [ "$color_prompt" = yes ]; then
PS1='$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m] [33[36m][t] [$ALERT_COLOR]$exit_status [33[0;37m]$ '
else
PS1='$debian_chroot:+($debian_chroot)u:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt


Everything works apart from the exit code. It never displays the exit code, and if I just do $? in my PS1, then it always shows as 0










share|improve this question









New contributor




Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Welcome to U&L StackExchange! When you say "if I just do $? in my PS1...", how did you do that?
    – JigglyNaga
    38 mins ago






  • 2




    Linking in the already-related: unix.stackexchange.com/questions/8396/…
    – Jeff Schaller
    36 mins ago










  • @JeffSchaller, urgh, the accepted answer there is just wrong
    – ilkkachu
    13 mins ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am running Ubuntu 18.04, and I would like to set my bash prompt so it reads like below:



user:~/Documents [14:22:07] 1 $


My PS1 is as follows:



ALERT_COLOR="$(tput setaf 1)"

# Display unsuccessful exit codes
function exit_status
last_status=$?
if [[ $last_status != 0 ]]; then
echo "$ALERT_COLOR[$last_status]"
fi


parse_git_branch() sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'

if [ "$color_prompt" = yes ]; then
PS1='$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m] [33[36m][t] [$ALERT_COLOR]$exit_status [33[0;37m]$ '
else
PS1='$debian_chroot:+($debian_chroot)u:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt


Everything works apart from the exit code. It never displays the exit code, and if I just do $? in my PS1, then it always shows as 0










share|improve this question









New contributor




Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am running Ubuntu 18.04, and I would like to set my bash prompt so it reads like below:



user:~/Documents [14:22:07] 1 $


My PS1 is as follows:



ALERT_COLOR="$(tput setaf 1)"

# Display unsuccessful exit codes
function exit_status
last_status=$?
if [[ $last_status != 0 ]]; then
echo "$ALERT_COLOR[$last_status]"
fi


parse_git_branch() sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'

if [ "$color_prompt" = yes ]; then
PS1='$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m] [33[36m][t] [$ALERT_COLOR]$exit_status [33[0;37m]$ '
else
PS1='$debian_chroot:+($debian_chroot)u:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt


Everything works apart from the exit code. It never displays the exit code, and if I just do $? in my PS1, then it always shows as 0







bash prompt bashrc






share|improve this question









New contributor




Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 38 mins ago









Goro

2,91741949




2,91741949






New contributor




Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 45 mins ago









Luke

112




112




New contributor




Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Luke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Welcome to U&L StackExchange! When you say "if I just do $? in my PS1...", how did you do that?
    – JigglyNaga
    38 mins ago






  • 2




    Linking in the already-related: unix.stackexchange.com/questions/8396/…
    – Jeff Schaller
    36 mins ago










  • @JeffSchaller, urgh, the accepted answer there is just wrong
    – ilkkachu
    13 mins ago
















  • Welcome to U&L StackExchange! When you say "if I just do $? in my PS1...", how did you do that?
    – JigglyNaga
    38 mins ago






  • 2




    Linking in the already-related: unix.stackexchange.com/questions/8396/…
    – Jeff Schaller
    36 mins ago










  • @JeffSchaller, urgh, the accepted answer there is just wrong
    – ilkkachu
    13 mins ago















Welcome to U&L StackExchange! When you say "if I just do $? in my PS1...", how did you do that?
– JigglyNaga
38 mins ago




Welcome to U&L StackExchange! When you say "if I just do $? in my PS1...", how did you do that?
– JigglyNaga
38 mins ago




2




2




Linking in the already-related: unix.stackexchange.com/questions/8396/…
– Jeff Schaller
36 mins ago




Linking in the already-related: unix.stackexchange.com/questions/8396/…
– Jeff Schaller
36 mins ago












@JeffSchaller, urgh, the accepted answer there is just wrong
– ilkkachu
13 mins ago




@JeffSchaller, urgh, the accepted answer there is just wrong
– ilkkachu
13 mins ago










1 Answer
1






active

oldest

votes

















up vote
5
down vote













The issue is here:



PS1='...[$ALERT_COLOR]$exit_status ...$ '
^^


That is a parameter expansion, it doesn't call the function you've set up.
You need to call the function within a command substitution, e.g. $(exit_status), or from PROMPT_COMMAND. If you do, take care with the [ .. ] escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).



And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:




In addition, the following table describes the special characters which can appear in the prompt variables PS1 to PS4: [...]
After the string is decoded, it is expanded via parameter expansion, command substitution, [...]




Something like this should work:



normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color

set_exit_color()
if [ "$?" != 0 ]; then
exit_color=$red_color
else
exit_color=$normal_color
fi


PROMPT_COMMAND=set_exit_color
PS1='[$exit_color][$?][$normal_color] w$ '


I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND doesn't modify the value of $? that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:



normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color

exit_color()
exit_code=$?
if [ "$exit_code" != 0 ]; then
echo "$red_color"
else
echo "$normal_color"
fi
return "$exit_code"


PS1='[$(exit_color)][$?][$normal_color] w$ '


I would use the version with PROMPT_COMMAND, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.






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



    );






    Luke is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f470018%2fset-last-command-exit-code-to-bash-prompt%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    The issue is here:



    PS1='...[$ALERT_COLOR]$exit_status ...$ '
    ^^


    That is a parameter expansion, it doesn't call the function you've set up.
    You need to call the function within a command substitution, e.g. $(exit_status), or from PROMPT_COMMAND. If you do, take care with the [ .. ] escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).



    And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:




    In addition, the following table describes the special characters which can appear in the prompt variables PS1 to PS4: [...]
    After the string is decoded, it is expanded via parameter expansion, command substitution, [...]




    Something like this should work:



    normal_color=$'33[00m'
    red_color=$'33[41m'
    exit_color=$normal_color

    set_exit_color()
    if [ "$?" != 0 ]; then
    exit_color=$red_color
    else
    exit_color=$normal_color
    fi


    PROMPT_COMMAND=set_exit_color
    PS1='[$exit_color][$?][$normal_color] w$ '


    I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND doesn't modify the value of $? that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:



    normal_color=$'33[00m'
    red_color=$'33[41m'
    exit_color=$normal_color

    exit_color()
    exit_code=$?
    if [ "$exit_code" != 0 ]; then
    echo "$red_color"
    else
    echo "$normal_color"
    fi
    return "$exit_code"


    PS1='[$(exit_color)][$?][$normal_color] w$ '


    I would use the version with PROMPT_COMMAND, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.






    share|improve this answer


























      up vote
      5
      down vote













      The issue is here:



      PS1='...[$ALERT_COLOR]$exit_status ...$ '
      ^^


      That is a parameter expansion, it doesn't call the function you've set up.
      You need to call the function within a command substitution, e.g. $(exit_status), or from PROMPT_COMMAND. If you do, take care with the [ .. ] escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).



      And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:




      In addition, the following table describes the special characters which can appear in the prompt variables PS1 to PS4: [...]
      After the string is decoded, it is expanded via parameter expansion, command substitution, [...]




      Something like this should work:



      normal_color=$'33[00m'
      red_color=$'33[41m'
      exit_color=$normal_color

      set_exit_color()
      if [ "$?" != 0 ]; then
      exit_color=$red_color
      else
      exit_color=$normal_color
      fi


      PROMPT_COMMAND=set_exit_color
      PS1='[$exit_color][$?][$normal_color] w$ '


      I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND doesn't modify the value of $? that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:



      normal_color=$'33[00m'
      red_color=$'33[41m'
      exit_color=$normal_color

      exit_color()
      exit_code=$?
      if [ "$exit_code" != 0 ]; then
      echo "$red_color"
      else
      echo "$normal_color"
      fi
      return "$exit_code"


      PS1='[$(exit_color)][$?][$normal_color] w$ '


      I would use the version with PROMPT_COMMAND, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.






      share|improve this answer
























        up vote
        5
        down vote










        up vote
        5
        down vote









        The issue is here:



        PS1='...[$ALERT_COLOR]$exit_status ...$ '
        ^^


        That is a parameter expansion, it doesn't call the function you've set up.
        You need to call the function within a command substitution, e.g. $(exit_status), or from PROMPT_COMMAND. If you do, take care with the [ .. ] escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).



        And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:




        In addition, the following table describes the special characters which can appear in the prompt variables PS1 to PS4: [...]
        After the string is decoded, it is expanded via parameter expansion, command substitution, [...]




        Something like this should work:



        normal_color=$'33[00m'
        red_color=$'33[41m'
        exit_color=$normal_color

        set_exit_color()
        if [ "$?" != 0 ]; then
        exit_color=$red_color
        else
        exit_color=$normal_color
        fi


        PROMPT_COMMAND=set_exit_color
        PS1='[$exit_color][$?][$normal_color] w$ '


        I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND doesn't modify the value of $? that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:



        normal_color=$'33[00m'
        red_color=$'33[41m'
        exit_color=$normal_color

        exit_color()
        exit_code=$?
        if [ "$exit_code" != 0 ]; then
        echo "$red_color"
        else
        echo "$normal_color"
        fi
        return "$exit_code"


        PS1='[$(exit_color)][$?][$normal_color] w$ '


        I would use the version with PROMPT_COMMAND, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.






        share|improve this answer














        The issue is here:



        PS1='...[$ALERT_COLOR]$exit_status ...$ '
        ^^


        That is a parameter expansion, it doesn't call the function you've set up.
        You need to call the function within a command substitution, e.g. $(exit_status), or from PROMPT_COMMAND. If you do, take care with the [ .. ] escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).



        And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:




        In addition, the following table describes the special characters which can appear in the prompt variables PS1 to PS4: [...]
        After the string is decoded, it is expanded via parameter expansion, command substitution, [...]




        Something like this should work:



        normal_color=$'33[00m'
        red_color=$'33[41m'
        exit_color=$normal_color

        set_exit_color()
        if [ "$?" != 0 ]; then
        exit_color=$red_color
        else
        exit_color=$normal_color
        fi


        PROMPT_COMMAND=set_exit_color
        PS1='[$exit_color][$?][$normal_color] w$ '


        I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND doesn't modify the value of $? that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:



        normal_color=$'33[00m'
        red_color=$'33[41m'
        exit_color=$normal_color

        exit_color()
        exit_code=$?
        if [ "$exit_code" != 0 ]; then
        echo "$red_color"
        else
        echo "$normal_color"
        fi
        return "$exit_code"


        PS1='[$(exit_color)][$?][$normal_color] w$ '


        I would use the version with PROMPT_COMMAND, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 28 mins ago

























        answered 35 mins ago









        ilkkachu

        51.2k678141




        51.2k678141




















            Luke is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Luke is a new contributor. Be nice, and check out our Code of Conduct.












            Luke is a new contributor. Be nice, and check out our Code of Conduct.











            Luke is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f470018%2fset-last-command-exit-code-to-bash-prompt%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke