Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8366729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:59:16+00:00 2026-06-09T12:59:16+00:00

I currently have a prompt in bash that calls a function to output the

  • 0

I currently have a prompt in bash that calls a function to output the return code of the last command run (if non-zero):

exit_code_prompt()
{
    local exit_code=$?
    if [ $exit_code -ne 0 ]
    then
        tput setaf 1
        printf "%s" $exit_code
        tput sgr0
    fi
}


PS1='$(exit_code_prompt)\$ '

This works rather nicely, except for $? not resetting unless another command is run:

$ echo "works"
works
$ command_not_found
bash: command_not_found: command not found
127$ 
127$ 
127$ 
127$ echo "works"
works
$

Is it possible to reset/unset the value of $? for the parent shell the first time exit_code_prompt() is run such that it does not continue to repeat the value in the prompt?

Many thanks, Steve.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:59:18+00:00Added an answer on June 9, 2026 at 12:59 pm

    The issue is that if you don’t issue another command, $? isn’t changing. So when your prompt gets reevaluated, it is correctly emitting 127. There isn’t really a workaround for this except manually typing another command at the prompt.

    edit: Actually I lied, there are always ways to store state, so you can store the value of $? and check if it’s changed, and clear the prompt if it has. But since you’re in a subshell, your options are pretty limited: You’d have to use a file or something equally dirty to store the value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an open instance of the command prompt, it reads java -cp
In my report I currently have a prompt box that displays a list of
Currently I have a value prompt added to my report (with UI selected as
Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have one project that currently contains multiple packages. These packages make up
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
We currently have an MSI that is created with WiX 3.5. The application is
I currently have my ZSH PROMPT variable set up as: PROMPT=$'[%{\e[0;32m%}%n@%m:%~%{\e[0m%}]> and my RPROMPT
I have java application which I am running on Unix from the command prompt.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.