2

I'm struggling with zsh prompt, trying to convert my bash prompt to zsh hooks.

This code should display the current kerberos ticket and update accordingly, but colors don't work, neither does the update:

# initialize advanced prompt
autoload -U promptinit && promptinit
# allow functions in prompt
setopt prompt_subst

#initialize colors
autoload -U colors && colors

# add the add-zsh-hook command
autoload add-zsh-hook

# Configure History
setopt hist_ignore_all_dups
setopt inc_append_history
export HISTIGNORE="history"

source $HOME/.zsh/prompt_krb.zsh

update_krb_env() {
if [ "`which klist 2>/dev/null`" != "" ]; then

    # got kerberos support installed
    ticket=`klist 2>/dev/null | grep "Principal:" | sed 's/.*Principal:\ //'`;

    klist -s
    if [ $? -eq 0 ]; then
        __KRB_PROMPT="%{$fg_no_bold['green']%}krb id \"$ticket\"";
    else
        if [ "$ticket" != "" ]; then
            __KRB_PROMPT="%{$fg_no_bold['orange']%}expired krb id \"$ticket\"";
        else
            __KRB_PROMPT="%{$fg_no_bold['yellow']%}no krb id";
        fi
    fi
else
    __KRB_PROMPT="%{$fg_no_bold['yellow']%}no krb id";
fi

_KRB_PROMPT="${__KRB_PROMPT}%{$reset_color%}";
}

krb_hook_preexec() {
    case "$2" in
        kinit*|kdestroy*)
            __EXECUTED_KRB_CMD=1;
        ;;
    esac
}

krb_hook_postexec() {
    if [ -n "$__EXECUTED_KRB_CMD" ]; then
        update_krb_env;
        unset __EXECUTED_KRB_CMD;
    fi
}

add-zsh-hook preexec krb_hook_preexec
add-zsh-hook precmd krb_hook_postexec
update_krb_env;

get_krb() {
    echo -n $__KRB_PROMPT;
}

PROMPT="$(get_krb) >"

I have been looking into multiple tutorials, trying to build upon the code, but I seem to be missing something.

4

Command substitutions and variables are expanded inside double quotes, so in

PROMPT="$(get_krb) >"

The $(get_krb) is expanded at the time of that assignment, while you want it to be expanded at each prompt via prompt_subst. So:

PROMPT='$(get_krb) >'
  • This helped with the updates, but colors still don't work. – Let_Me_Be Mar 9 '14 at 17:06
  • Nevermind :-D Colors were again caused by the silly quotes. Thx for help. – Let_Me_Be Mar 9 '14 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.