Skip to content

Instantly share code, notes, and snippets.

@galaran
Last active December 16, 2020 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galaran/e23af1cf70accef50299aae664f54469 to your computer and use it in GitHub Desktop.
Save galaran/e23af1cf70accef50299aae664f54469 to your computer and use it in GitHub Desktop.
Start ssh-agent on opening shell, if not running yet (Git bash environment)
# Based on script from Github Help:
# https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/working-with-ssh-key-passphrases#auto-launching-ssh-agent-on-git-for-windows
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
agent_start
fi
unset env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment