-if type keychain >/dev/null 2>&1 ; then
- : ${HOSTNAME:=$(hostname)}
- [ -e ~/.keychain/${HOSTNAME}-sh ] && . ~/.keychain/${HOSTNAME}-sh
- [ -e ~/.keychain/${HOSTNAME}-sh-gpg ] && . ~/.keychain/${HOSTNAME}-sh-gpg
+# If an auth sock is already set & available, use it.
+# This way ssh agent forwarding still works.
+if [ ! -S "${SSH_AUTH_SOCK}" ] ; then
+ if type keychain >/dev/null 2>&1 ; then
+ : ${HOSTNAME:=$(hostname)}
+ [ -e ~/.keychain/${HOSTNAME}-sh ] && . ~/.keychain/${HOSTNAME}-sh
+ [ -e ~/.keychain/${HOSTNAME}-sh-gpg ] && . ~/.keychain/${HOSTNAME}-sh-gpg
+ fi
fi