6

I want to include the return status in my prompt. (Easy add '$? ', right?)

However, I only want the status returned (and trailing space) if non-zero.

Example:

sd ~ $ false
1 sd ~ $ true
sd ~ $ 
Steven
  • 738

3 Answers3

4

It is not required to use PROMPT_COMMAND. Here it does needless complications. All you have to do is define this function:

prompt_status()
{
    [ $? = 0 ] && return
    echo -n "${?} "
}

And then set PS1 like this:

shopt -s promptvars
PS1='$(prompt_status)'$PS1

Evaluation of prompt_status() inside subshell protects $? from being changed.

midenok
  • 583
4

Make sure that the promptvars option is on (it is by default). Then put whatever code you like in PROMPT_COMMAND to define a variable containing exactly what you want in the prompt.

PROMPT_COMMAND='prompt_status="$? "; if [[ $prompt_status == "0 " ]]; then prompt_status=; fi'
PS1='$prompt_status\h \w \$ '

In zsh you could use its conditional construct in PS1 (bash has no equivalent).

PS1='%(?,,%? )%m %~ %# '
3

The best I can have is obtained by

PS1='${?/#0/}'":$PS1"

where I added a : as a separator, so not exactly what is in the question.

enzotib
  • 51,661