How to use ssh-agent with the fish shell

This article describes how to start the ssh-agent when using the fish shell.

When using github it is now a requirement to use the ssh-agent to login to the service. Otherwise new commits cannot be pushed to the remote repository. The github website only describes how to do set this up for the bash-like and c-like shells. If the same command is used for a fish shell it fails:

1
2
3
4
╭─shr@shr in ~ took 13m51s
╰─λ eval "$(ssh-agent -s)"

fish: Unsupported use of '='. In fish, please use 'set SSH_AUTH_SOCK /tmp/ssh-XXXXXXAXsPUo/agent.9287'.

The solution is to start the ssh-agent like for C-shells:

1
2
3
╭─shr@shr in repo: sroeschus.github.io/content/posts/stack_usage on  main [?] took 0s
╰─λ agent
Agent pid 7434

To make it easier to start the ssh-agent, it can be convenient to define a new shell alias to start the ssh agent:

1
2
3
╭─shr@shr in repo: sroeschus.github.io/content/posts/stack_usage on  main [?] took 0s
╰─λ alias --save agent="eval \$(ssh-agent -c)"
funcsave: wrote /home/shr/.config/fish/functions/agent.fish

Some developers might prefer to automatically start the ssh agent if its not already started. There is a great solution in this github repo.

Just follow the install instructions or do the following:

1
2
wget https://gitlab.com/kyb/fish_ssh_agent/raw/master/functions/fish_ssh_agent.fish \
      -P ~/.config/fish/functions/

This adds two functions to your ~/.config/fish/functions file. To load the agent when your shell starts, just add the following to your ~/.config/fish/config.fish file. This will automatically start the ssh agent.