Skip to content

Instantly share code, notes, and snippets.

@nouvist
Last active August 6, 2022 10:09
Show Gist options
  • Save nouvist/a8280d1ea6abd01fa5bb44a11454b89d to your computer and use it in GitHub Desktop.
Save nouvist/a8280d1ea6abd01fa5bb44a11454b89d to your computer and use it in GitHub Desktop.
SSH Agent with Bash Profile

⚠️ Use keychain instead, since that will setup the rest for you.

SSH Agent with Bash Profile

Add following script to your .bashrc or .zshrc.

# this will load existing pid and socket variable
source ~/.sshagent > /dev/null
echo "[info] ssh-agent already started"

# if the socket is not available, then init ssh-agent
if [[ ! -S "$SSH_AUTH_SOCK" ]]; then
  ssh-agent > ~/.sshagent
  source ~/.sshagent > /dev/null
  ssh-add
  echo "[info] starting ssh-agent"
fi

MINGW / MSYS2 / CYGWIN

NOTE: Windows is now have OpenSSH preinstalled. If you are using Windows, I recommend you to enable SSH Agent from services instead. Don't forget to setup Git to use external SSH.

For cygwin, check pid instead.

source ~/.sshagent > /dev/null
echo "[info] ssh-agent already started"

if [[ ! $(ps -sp $SSH_AGENT_PID | grep ssh-agent) ]]; then
  ssh-agent > ~/.sshagent
  source ~/.sshagent > /dev/null
  ssh-add
  echo "[info] starting ssh-agent"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment