Skip to content
CppCooperProgramming Portfolio

Article

A Terminal Environment That Remembers Its SSH Agent

CLI Tools

A terminal environment is more than a prompt and a set of aliases. It also carries the small bits of state that let the next command find the tools it needs. SSH-agent connections are a particularly useful example, because a running agent and a shell that knows how to reach it are two separate things.

The SSH helpers in my environment keep that connection information in a reusable form. They are also one of the parts I am particularly happy to have when moving the surrounding terminal setup around.

Keeping the connection between sessions

Starting an agent produces environment information, including SSH_AUTH_SOCK and SSH_AGENT_PID. The helpers save that information to an agent environment file. Another shell can source the file and acquire the connection details instead of relying on how its parent process happened to be started.

The startup configuration checks for user-owned agents and attempts to reconnect through the saved environment. It uses ssh-add -l when deciding whether the connection needs to be replaced. Agent discovery and process handling have separate Windows and Linux paths, keeping those differences out of the shell’s ordinary startup commands.

This is personal environment code, with the platform assumptions that come with that history. The presence of both paths describes the intended abstraction; it does not establish that every machine or shell combination has been exercised.

The same setup before a GUI launch

The connection matters outside the terminal too. A small Sublime Merge launcher sources the same SSH startup configuration, then executes the application. The child process receives the resulting environment.

That keeps the application launcher concerned with launching an application. Agent discovery, saved state and reconnection stay in the shared SSH setup, where terminal startup can use them as well.

The useful boundary is between establishing the environment and consuming it. A shell session and a graphical Git client need the same connection details, even though they enter the workflow in different ways. Putting that setup in one place makes the relationship explicit.