Skip to content

Instantly share code, notes, and snippets.

@andy-b-84
Last active October 15, 2025 09:48
Show Gist options
  • Save andy-b-84/f3a80bd5ef5e90145057e9cb27797f5c to your computer and use it in GitHub Desktop.
Save andy-b-84/f3a80bd5ef5e90145057e9cb27797f5c to your computer and use it in GitHub Desktop.
WSL ssh agent
# This .bahrc file is natively compatible with Ubuntu@WSL, not with git-bash (as it uses pgrep)
# Check if there is already an ssh-agent AUTH_SOCK configured in the environment
if [ "$SSH_AUTH_SOCK" = "" ]; then
SSH_AGENT_OUTPUT=$(cat ~/.ssh-agent-output)
# Check if an ssh-agent command's output has already been saved
if [ "$SSH_AGENT_OUTPUT" = "" ]; then
# If not : run a new agent
SSH_AGENT_OUTPUT=$(ssh-agent)
echo "$SSH_AGENT_OUTPUT" > ~/.ssh-agent-output
fi
# Populate environment with ssh-agent's output
eval "$SSH_AGENT_OUTPUT"
fi
# Check if what's been saved matches current processes
SSH_CURRENT_AGENT_PID=$(pgrep ssh-agent)
# git-bash variant below
# SSH_CURRENT_AGENT_PID=$(ps -ef | grep "ssh-agent" | grep -wv 'grep' | awk '{print $2}')
if [ "$SSH_AGENT_PID" != "$SSH_CURRENT_AGENT_PID" ]; then
# If not (for example : after a restart): launch a new ssh-agent
SSH_AGENT_OUTPUT=$(ssh-agent)
echo "$SSH_AGENT_OUTPUT" > ~/.ssh-agent-output
eval "$SSH_AGENT_OUTPUT"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment