This began with the want of adding SSH keys to ssh-agend on demand, but because this isn't possible with OpenSSH, I came up with this zsh script instead. But for a reason I can't figure out, instead of only running "before" ssh or git, it is run before anything, even empty commands.

git ssh () {
    # Does the agent handle any keys?
    ssh-add -l &>/dev/null

    # True if last executed command (ssh-add -l) exits non-zero.
    if [ $? -ne 0 ]; then

    # Execute git or ssh with all parameters, and not this function
    command $0 "$@"

Why does this function execute after every command instead of only when git or ssh is entered?

1 Answer 1


I tried the function with zsh -f (which I should have done before posting..), and found that it worked as expected. Something else is causing this issue, so I'm closing this question when able.


For those interested in what the issue was, and how it was solved, look below.

I used the zsh framework ZIM which uses vcs_info to check if the current directory is used by git or not. This requires a call to git. vcs_info itself does not call git directly, it would seem, not by default.

However, thanks to ft on #zsh on Freenode, adding

zstyle ':vcs_info:git:*:-all-' command =git

to ~/.zshrc after the framework has been sourced, has fixed this issue.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .