Skip to content
hook up with an operating or new SSH agent
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



"Socket to me, baby!"

This is a sourceable shell include file which provides a sshag function to conveniently hook up with an operating ssh-agent.

It will start a new agent session if it doesn't find an agent to connect with.

You might want to source it from within your ~/.bashrc file or other profile script.

It can also be run as an executable script, and its output stored in the relevant environment variable. This is particularly useful when it is desired to configure a non-shell environment, for example that of a text editor.

Messages are emitted on standard out; the output will always consist of just the socket location.



$ ssh
Enter passphrase for key '/home/austin/.ssh/id_dsa': ^C
$ source
$ sshag
    2048 0d:db:a1:1a:cc:01:ad:ec:ab:00:d1:ed:eb:ac:1e:00 /home/austin/.ssh/id_dsa (DSA)
$ ssh


$ export SSH_AGENT_SOCK=``
Output should be assigned to the environment variable $SSH_AUTH_SOCK.
    2048 0d:db:a1:1a:cc:01:ad:ec:ab:00:d1:ed:eb:ac:1e:00 /home/austin/.ssh/id_dsa (DSA)

Appended to ~/.bashrc: source ~/lib/sshag/; sshag &>/dev/null


This code was posted as a response to a question on As I understand it, code posted to that site is under the terms of the Creative Commons Attribution-Sharealike License, so I'm attributing it to the user Zed. SU currently links to version 2.5 of the license.

A copy of the full license is distributed herein in the file COPYING.

The basic gist of the license

You are free:

  • to Share — to copy, distribute and transmit the work
  • to Remix — to adapt the work

Under the following conditions:

  • Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
  • Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

With the understanding that:

  • Waiver — Any of the above conditions can be waived if you get permission from the copyright holder.

  • Public Domain — Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license.

  • Other Rights — In no way are any of the following rights affected by the license:

    • Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations;
    • The author's moral rights;
    • Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
  • Notice — For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.

You can’t perform that action at this time.