TIP Exit Status in prompt
From Gentoo Linux Wiki
| Terminals / Shells • Network • X Window System • Portage • System • Filesystems • Kernel • Other |
Contents |
[edit] Introduction
Did that previous command exit successfully? There is an easy way to know for sure.
[edit] bash
Put the following into your .bashrc:
| 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
|
[edit] 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
}
|
[edit] 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
[edit] 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.
| 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 |
[edit] See also
- http://linuxgazette.net/issue65/padala.html - more about colors in console
- http://zsh.sunsite.dk/Doc/Release/zsh_12.html - zsh prompt expansion

