Search:  
Gentoo Wiki

TIP_Exit_Status_in_prompt

This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other
It is requested that an image or additional image(s) be included in this article to improve its quality, if possible.

For more information, please refer to discussion on this page.

Contents

Introduction

Did that previous command exit successfully? There is an easy way to know for sure.

bash

Put the following into your .bashrc:

Effect of the additonal code
Effect of the additonal code
Code: Exit Status Bash Foo
COLOR_RED="\[\e[31;40m\]"
COLOR_GREEN="\[\e[32;40m\]"
COLOR_YELLOW="\[\e[33;40m\]"
COLOR_BLUE="\[\e[34;40m\]"
COLOR_MAGENTA="\[\e[35;40m\]"
COLOR_CYAN="\[\e[36;40m\]"

COLOR_RED_BOLD="\[\e[31;1m\]"
COLOR_GREEN_BOLD="\[\e[32;1m\]"
COLOR_YELLOW_BOLD="\[\e[33;1m\]"
COLOR_BLUE_BOLD="\[\e[34;1m\]"
COLOR_MAGENTA_BOLD="\[\e[35;1m\]"
COLOR_CYAN_BOLD="\[\e[36;1m\]"

COLOR_NONE="\[\e[0m\]"

promptFunc()
{
    PREV_RET_VAL=$?;

    PS1=""

    if test `whoami` != "root"
    then
        PS1="${PS1}${COLOR_CYAN_BOLD}\u${COLOR_NONE}"
    else
        PS1="${PS1}${COLOR_RED_BOLD}\u${COLOR_NONE}"
    fi

    PS1="${PS1}@\h"

    if test $PREV_RET_VAL -eq 0
    then
        PS1="${PS1}${COLOR_GREEN_BOLD}\\$ ${COLOR_NONE}"
    else
        PS1="${PS1}${COLOR_RED_BOLD}\\$ [${PREV_RET_VAL}] ${COLOR_NONE}"
    fi
}

PROMPT_COMMAND=promptFunc

default colors

If you prefer original bash prompt, replace promptFunc with the following code:

Code: promptFunc
promptFunc()
{
    PREV_RET_VAL=$?;

    PS1=""

    if test `whoami` != "root"
    then
        PS1="${PS1}${COLOR_GREEN_BOLD}\u@\h ${COLOR_BLUE_BOLD}\w"
    else
        PS1="${PS1}${COLOR_RED_BOLD}\h ${COLOR_BLUE_BOLD}\W"
    fi

    if test $PREV_RET_VAL -eq 0
    then
        PS1="${PS1} ${COLOR_BLUE_BOLD}\\$ ${COLOR_NONE}"
    else
        PS1="${PS1} ${COLOR_RED_BOLD}[${PREV_RET_VAL}] \\$ ${COLOR_NONE}"
    fi
}

Show only on following prompt

With either of the above versions, you get something like this:

  jkaufman@tux ~ $ not_a_command
  bash: not_a_command: command not found
  jkaufman@tux ~ $ [127] <Ctrl-C>
  jkaufman@tux ~ $ [127]

But I want:

  jkaufman@tux ~ $ not_a_command
  bash: not_a_command: command not found
  jkaufman@tux ~ $ [127] <Ctrl-C>
  jkaufman@tux ~ $ 

I want to be able to do <Ctrl-C><Ctrl-L> and always get a clean looking terminal. So I use $HISTCMD to let me know if I'm reporting something the user already knows:

Code: promptFunc
PREV_HISTCMD=-1
promptFunc()
{
    PREV_RET_VAL=$?;

    PS1=""

    if test `whoami` != "root"
    then
        PS1="${PS1}${COLOR_GREEN_BOLD}\u@\h ${COLOR_BLUE_BOLD}\w"
    else
        PS1="${PS1}${COLOR_RED_BOLD}\h ${COLOR_BLUE_BOLD}\W"
    fi

    if test $PREV_RET_VAL -eq 0 || test $HISTCMD -eq $PREV_HISTCMD
    then
        PS1="${PS1} ${COLOR_BLUE_BOLD}\\$ ${COLOR_NONE}"
    else
        PS1="${PS1} ${COLOR_RED_BOLD}[${PREV_RET_VAL}] \\$ ${COLOR_NONE}"
    fi

    PREV_HISTCMD=$HISTCMD
}

zsh

Put the following into your .zshrc:

Code: Put into .zshrc
COLOR_RED="^[[31;40m"
COLOR_GREEN="^[[32;40m"
COLOR_YELLOW="^[[33;40m"
COLOR_BLUE="^[[34;40m"
COLOR_MAGENTA="^[[35;40m"
COLOR_CYAN="^[[36;40m"

COLOR_RED_BOLD="^[[31;1m"
COLOR_GREEN_BOLD="^[[32;1m"
COLOR_YELLOW_BOLD="^[[33;1m"
COLOR_BLUE_BOLD="^[[34;1m"
COLOR_MAGENTA_BOLD="^[[35;1m"
COLOR_CYAN_BOLD="^[[36;1m"

COLOR_NONE="^[[0m"

precmd()
{
    PREV_RET_VAL=$?;
    PS1=""
    if test `whoami` != "root"
    then
        PS1="${PS1}${COLOR_GREEN_BOLD}%n@%m ${COLOR_BLUE_BOLD}%~"
    else
        PS1="${PS1}${COLOR_RED_BOLD}%m ${COLOR_BLUE_BOLD}%1~"
    fi

    if test $PREV_RET_VAL -eq 0
    then
        PS1="${PS1} ${COLOR_BLUE_BOLD}%# ${COLOR_NONE}"
    else
        PS1="${PS1} ${COLOR_RED_BOLD}[%?] %# ${COLOR_NONE}"
    fi
}
The symbol ^[ you can get by using vim and type in CTRL+V ESC

zsh alternative

The following file is basically the same as in /usr/share/zsh/$ZSH_VERSION/functions/Prompts/prompt_gentoo_setup but enhanced to display the exit status of the previous command if it is other than zero. If the command was aborted with a signal then the signal name is displayed.

Demonstration of the modified prompt_gentoo_setup
Demonstration of the modified prompt_gentoo_setup
File: ~/.zsh/functions/prompt_gentoo_setup
# gentoo prompt theme

prompt_gentoo_help () {
  cat <<'EOF'
This prompt is color-scheme-able.  You can invoke it thus:

  prompt gentoo [<promptcolor> [<usercolor> [<rootcolor> [<userstatuscolor> [   <rootstatuscolor>]]]]]

EOF
}

prompt_gentoo_precmd () {
  local exitstatus=$?

  if [ $exitstatus -ne 0 ] ; then
    if [ $exitstatus -gt 128 -a $exitstatus -lt 163 ] ; then
      psvar=("SIG$signals[$exitstatus-127]")
    else
      psvar=("$exitstatus")
    fi
  else
    psvar=()
  fi
}

prompt_gentoo_setup () {
  prompt_gentoo_prompt=${1:-'blue'}
  prompt_gentoo_user=${2:-'green'}
  prompt_gentoo_root=${3:-'red'}
  prompt_gentoo_user_status=${4:-'red'}
  prompt_gentoo_root_status=${5:-'cyan'}
  
  if [ "$USER" = 'root' ]
  then
    base_prompt="%{$fg_bold[$prompt_gentoo_root]%}%m%{$reset_color%} "
    status_prompt="%(1v.%{$fg_bold[$prompt_gentoo_root_status]%}[%v]%{$reset_color%} .)"
  else
    base_prompt="%{$fg_bold[$prompt_gentoo_user]%}%n@%m%{$reset_color%} "
    status_prompt="%(1v.%{$fg_bold[$prompt_gentoo_user_status]%}[%v]%{$reset_color%} .)"
  fi
  post_prompt="%{$reset_color%}"

  local color="%{*}"
  base_prompt_no_color="${(S)base_prompt//${~color}/}"
  post_prompt_no_color="${(S)post_prompt//${~color}/}"

  setopt noxtrace localoptions
  local base_prompt_expanded_no_color base_prompt_etc
  local prompt_length space_left

  base_prompt_expanded_no_color=$(print -P "$base_prompt_no_color")
  base_prompt_etc=$(print -P "$base_prompt%(4~|...|)%3~")
  prompt_length=${#base_prompt_etc}
  path_prompt="%{$fg_bold[$prompt_gentoo_prompt]%}%1~"

  PS1="$status_prompt$base_prompt$path_prompt %# $post_prompt"
  PS2="$status_prompt$base_prompt$path_prompt %_> $post_prompt"
  PS3="$status_prompt$base_prompt$path_prompt ?# $post_prompt"

  functions[precmd]="prompt_gentoo_precmd
      ${functions[precmd]//prompt_*_precmd}"
  functions[preexec]="${functions[preexec]//prompt_*_preexec}"
}

prompt_gentoo_setup "$@"


The prompt updating logic is included in the prompt_gentoo_precmd function which could be easily adapted to include other information (i.e. laptop battery status or number of jobs running in background). Note, that it is inserted into a probably already defined precmd function, so it is possible to define the function precmd and later switch the prompt without overwriting precmd.

The first 9 elements of the psvar array can be referenced directly in the prompt. '%v' only references the first element of psvar, for using more values you would write '%1v' for psvar[1], '$2v' for psvar[2], and so on. The $status_prompt string uses a prompt conditional, which inserts the first element of the psvar array, if there is at least one element in the array (1v).

Put the following into your .zshrc file to use this:

Code: Put into .zshrc
export ZSHDIR=$HOME/.zsh

# for custom functions
fpath=($ZSHDIR/functions $fpath)

# needs to be done after precmd/preexec are defined!
promptinit; prompt gentoo

See also

Retrieved from "http://www.gentoo-wiki.info/TIP_Exit_Status_in_prompt"

Last modified: Wed, 01 Oct 2008 07:58:00 +0000 Hits: 12,379